Rails Generate Migration
You can generate a rails migration file from the console using this formula:
rails generate migration AddFieldsToModels field_name:field_type field_name_2:field_type
For example, if you wanted to add the first_name
and last_name
fields to the users
table, you can do this:
rails generate migration AddNamesToUsers last_name:string first_name:string
Rails will create this file:
class AddNamesToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :last_name, :string
add_column :users, :first_name, :string
end
end
Now, migrate the database by running rake db:migrate
in the console.
Note: For even less typing, you can replace generate
with g
.
rails generate model
To easily generate an ActiveRecord model that inherits from the right class in Rails and automagically creates the correct db migrations & boilerplate test files for your model, simply do:
rails generate model NAME lots_of_options
Options include the name of the column and the type (e.g. name:string
or body:text
).
You can also setup foreign keys easily by doing belongs_to:model_name
.
So say you wanted to setup a User
model that has a username
, email
and belongs to a School
, you could do something like this:
rails generate model User username:string email:string school:belongs_to
You could also simply do rails g
instead of spelling out rails generate
. So the above would look like this:
rails g model User username:string email:string school:belongs_to
rails generate controller
we can easily generate controller with rails g controller
command.
example
$ bin/rails generate controller controller_name
The controller generator is expecting parameters in the form of generate controller ControllerName action1 action2
.
Let's make a Greetings controller with an action of hello, which will say something nice to us.
$ bin/rails generate controller Greetings hello
create app/controllers/greetings_controller.rb
route get "greetings/hello"
invoke erb
create app/views/greetings
create app/views/greetings/hello.html.erb
invoke test_unit
create test/controllers/greetings_controller_test.rb
invoke helper
create app/helpers/greetings_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/greetings.coffee
invoke scss
create app/assets/stylesheets/greetings.scss
it will generate controller file
, a view file
, a functional test file
, a helper
for the view, a JavaScript file
and a stylesheet file
, as well as adding the routes for each action to routes.rb
.
rails generate scaffold
DISCLAIMER: Scaffolding is not recommended unless it's for very conventional CRUD apps/testing. This may generate a lot of files(views/models/controllers) that are not needed in your web application thus causing headaches(bad :().
You can generate a fully working scaffold for a new object, including model, controller, views, assets, and tests by using the rails g scaffold
command.
For example:
$ rails g scaffold Widget name:string price:decimal invoke active_record create db/migrate/20160722171221_create_widgets.rb create app/models/widget.rb invoke test_unit create test/models/widget_test.rb create test/fixtures/widgets.yml invoke resource_route route resources :widgets invoke scaffold_controller create app/controllers/widgets_controller.rb invoke erb create app/views/widgets create app/views/widgets/index.html.erb create app/views/widgets/edit.html.erb create app/views/widgets/show.html.erb create app/views/widgets/new.html.erb create app/views/widgets/_form.html.erb invoke test_unit create test/controllers/widgets_controller_test.rb invoke helper create app/helpers/widgets_helper.rb invoke jbuilder create app/views/widgets/index.json.jbuilder create app/views/widgets/show.json.jbuilder invoke assets invoke javascript create app/assets/javascripts/widgets.js invoke scss create app/assets/stylesheets/widgets.scss
Then you can run rake db:migrate
to set up the database table.
Then you can visit http://localhost:3000/widgets and you'll see a fully functional CRUD scaffold.