Rails Flashcards
What are helpers for and where do you place them?
A helper is a module, as opposed to class. Helpers are a way to access methods, that a view would otherwise not have access to, and a way to keep the method code out of the view files. Helpers are placed in the /helpers folder, and must be named using the “_helper.rb” format.
What are ‘concerns’ in Rails?
The are two concerns folders in a Rails app, one in the models folder and one in the controllers folder. The concerns folders are for bits of code that can be shared by controllers or models, thus avoiding recreating the same methods for multiple unrelated classes.
How do you start the rails server, and where are the web pages served:
Use ‘rails server’ or ‘rails s’ to start the server. The website can be found at http://localhost:3000.
How do you create a basic rails app?
Use the ‘rails new AppName’ command. You can view the list of options by typing ‘rails new -h’ into the terminal.
What is the ‘rails g’ command for?
‘rails generate’, uses templates to generate bolierplate code for lots of different uses. Most commonly used options are ‘controller’, ‘model’, ‘scaffold’, and ‘migration’. The full list of options can be found by running the ‘rails g’ command.
How do you generate a new controller for a rails app?
‘rails g controller [controller name] [action1 action2 …] [options]’
How do you generate a new model for a rails app?
‘rails g model [modelname] [fieldname:type] [fieldname:type] …’
How are URLs structured in a Rails app?
http://[host}/[controller]/[action]
When you generate a controller with the rails command, what files are created?
A controller file, a view file, a functional test file, a helper for the view, a JavaScript file and a stylesheet file.
How do you create a model and its controller at the same time?
‘rails g scaffold Name fieldname:type fieldname:type …’
How do you update the database after generating a new model?
run ‘rake db:migrate’
What does ‘rails console’ do?
Its the same as irb, it starts an interactive ruby session. Can also be called using ‘rails c’.
How can you access the database from the commandline?
‘rails dbconsole’ or ‘rails db’ will figures out which database you’re using and drop you into whichever command line interface you would use with it.
How do you remove a model or controller from a Rails app?
‘rails d model [modelname]’ or ‘rails d controller [controllername]’. You can also use ‘rails destroy’ instead of ‘rails d’.
How can you get version information about the copy of Ruby, RubyGems, etc?
‘rake about’