rails Flashcards
Why is the view of Rails application in the format *.erb.html? What does “erb” mean?
erb stands for “Embedded RuBy”. A .html.erb or .erb.html file is HTML with Ruby code embedded in; Rails will evaluate the Ruby to add content to the file dynamically, and will output a “pure” HTML file for rendering.
create a new ruby app named demo
sudo rails new demo –skip-bundle
add less before installing bundle
gem ‘therubyracer’gem ‘less-rails’(added in Gemfile)
update bundle
sudo bundle updatesudo rails server
create a new ‘todo’ controller, use ‘index’ as default page, ‘add’ and ‘delete’ as other view pages
sudo rails g controller Todos index add delete# Controller name is always plural as per convention. Todos instead of Todo, Tweets instead of Tweet, Blogs instead of Blog.
what is a rails console and how to enter?
rails consoleRails console loads the complete Rails app environment. All the model and controller classes of the Rails app is available inside Rails console.
what is a model?
When you create a model, the name is singular and the name starts with capital letter. Todo, instead of Todos.To get started, it’s important to understand that Rails is built on MVC architecture; that is, Model–View–Controller.Models are responsible for handling data storage and the business logic. Views are responsible for the layer that the user deals with. Controllers act as the bridge between the Models and the Views. In other words, Views generate your HTML, Models handle your database interactions (and any logic based on that data), while Controllers tie the two together, and are where you place logic that controls where the browser is redirected and what the user can do.
how to create a model and undo the action?
rails g[enerate] model Todo todo_item:stringrails d[estroy] model TodoHowever if you have executed rake db:migrate, you need to execute “rake db:rollback”. This will remove the table and all the data from your database as well.
what is Migration?
Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don’t have to write SQL by hand, allowing your schema and changes to be database independent.You can think of each migration as being a new ‘version’ of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version. Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.
how to use migration?
rake db:migratethe command pushes the database changes from the migration file to the actual database. In Rails the default database is sqlite.One of the purpose of Rails model is to shield us from dealing directly with sql database. Writing sql queries for every database task is tedious and rails handles all that for us.Tip You would hardly need to refer to the actual table by the name todos. You would only be accessing the data through the model Todo.
how to add items into database through rails console: create a todo_item named “sing”
Todo.create(:todo_item => “sing”)
how to access Models(database) in controller?
def index @todo_items = Todo.all end
how to create a new view file: app/views/todos/delete.html.erb
touch app/views/todos/delete.html.erb
the possible Model commands:
Todo.allTodo.deleteTodo.delete_allTodo.create(:todo_item => “xxx”)t = Todo.newt.todo_item = “xxx”t.saveTodo.firstTodo.lastTodo.find(5)Todo.find_by_todo_item(“xxx”)
how to add link in the a tag, in the same project?
DeleteBack