
If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, resources or scaffolding, and what files are created by each command.
Say you want to generate a Test model with a name. You could either generate the model individually, generate resources, or generate scaffolding, as follows:
rails g model Test name:text rails g resource Test name:text rails g scaffold Test name:text
What’s the difference between each of the above?
Generating Individual Models
Entering rails g model Test name:text in your command line will generate the following:
(1) A model file test.rb in your models directory:
class Test < ActiveRecord::Base end
(2) A migration file timestamp_create_tests.rb in your db/migrate directory:
class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name
t.timestamps
end
end
end
Generating Resources
Entering rails g resource Test name:text in your command line will generate the following:
(1) A model file test.rb in your models directory:
class Test < ActiveRecord::Base end
(2) A migration file timestamp_create_tests.rb in your db/migrate directory:
class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name
t.timestamps
end
end
end
(3) a tests_controller.rb file in your controllers directory. This controller will be an empty shell:
class TestsController < ApplicationController end
(4) resources :tests routes in your routes.rb file.
Generating Scaffolding
Entering rails g scaffold Test name:text in your command line will generate the following:
(1) A model file test.rb in your models directory:
class Test < ActiveRecord::Base end
(2) A migration file timestamp_create_tests.rb in your db/migrate directory:
class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name
t.timestamps
end
end
end
(3) A tests_controller.rb file in your controllers directory. When a scaffold is generated, seven public methods and two private methods will be added to your controller:
class TestsController < ApplicationController
before_action :set_test, only: [:show, :edit, :update, :destroy]
# GET /tests
# GET /tests.json
def index
@tests = Test.all
end
# GET /tests/1
# GET /tests/1.json
def show
end
# GET /tests/new
def new
@test = Test.new
end
# GET /tests/1/edit
def edit
end
# POST /tests
# POST /tests.json
def create
@test = Test.new(test_params)
respond_to do |format|
if @test.save
format.html { redirect_to @test, notice: 'Test was successfully created.' }
format.json { render action: 'show', status: :created, location: @test }
else
format.html { render action: 'new' }
format.json { render json: @test.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tests/1
# PATCH/PUT /tests/1.json
def update
respond_to do |format|
if @test.update(test_params)
format.html { redirect_to @test, notice: 'Test was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @test.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tests/1
# DELETE /tests/1.json
def destroy
@test.destroy
respond_to do |format|
format.html { redirect_to tests_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_test
@test = Test.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def test_params
params.require(:test).permit(:name)
end
end
(4) resources :tests routes in your routes.rb file.
(5) Seven corresponding view files in your views directory: (a) _form.html.erb, (b) edit.html.erb, (c) index.html.erb, (d) index.json.jbuilder, (e) new.html.erb, (f) show.html.erb and (g) show.json.jbuilder. Each view will contain html and embedded ruby.
To learn more about the routes generated by resources, please read Getting Started with Ruby on Rails.
- PM Career Story - April 28, 2022
- How to Transition into Product Management - December 26, 2017
- What I’ve Learned in My First Few Months as a Product Manager - October 14, 2015
Super helpful, thanks Koren!
Happy to help! 😉
I used this today!
Awesome, Randall!
Excellent post showing the differences between the various rails generators. Clear and concise.
Thanks, happy to help!
Very Helpful! Thanks!
No problem! 😉
Very clarifying info. Didn’t know about the resource command.
Happy to help!
Very clear and to the point. Excellent explanation, love the examples of the output files. Thanks for this.
Awesome! 🙂
Very helpful, and concise! Thank you!
Happy to help!
Really awesome…….thank you so much………..thanks alot…i am fresher ,i started my life with ruby on rails so please suggest me and give some helpfull thing to me….my mail_id: bssp89@gmail.com
You’re welcome!
Thanks for this!
No prob! 😉
Thanks for such a nice explanation
i like this article because it is idiot proof 🙂
finally , someone did and told us the difference …
new to rails but i think and everyday i like it more ..
thank u very much for your kindness .
🙂
Thanks, I did not know about the resource command option, never used it till today. This help is awesome!
Cheers for clearing that one up! 😀
Wow, I just get into Rails and this is really helpful for me. Thank you so much!
I need to know the exact routes. Not the resources :… The get, post thing. Help please.
Thank You, Koren it was well worth the readand bookmarked
You’re welcome!
this is the best explanation I’ve seen so far, thank you so much Koren