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?

Entering text in your command line will generate the following:

(1) A model file models directory:

class Test < ActiveRecord::Base
end

(2) A migration file migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name
 
      t.timestamps
    end
  end
end


 

Entering text in your command line will generate the following:

(1) A model file models directory:

class Test < ActiveRecord::Base
end

(2) A migration file migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name
 
      t.timestamps
    end
  end
end

(3) a controllers directory.  This controller will be an empty shell:

class TestsController < ApplicationController
end

 (4) rb file.

 



 

 

Entering text in your command line will generate the following:

(1) A model file models directory:

class Test < ActiveRecord::Base
end

(2) A migration file migrate directory:

class CreateTests < ActiveRecord::Migration
  def change
    create_table :tests do |t|
      t.text :name
 
      t.timestamps
    end
  end
end

(3) A controllers directory.  When a scaffold is generated, seven public methods and two private methods will be added to your controller:

(4) rb file.

(5) Seven corresponding view files in your  jbuilder. Each view will contain html and embedded ruby.

相关文章:

  • 2021-11-08
  • 2021-12-18
  • 2021-06-01
  • 2021-08-28
  • 2021-08-01
猜你喜欢
  • 2021-06-15
  • 2021-07-29
  • 2022-01-15
  • 2021-08-31
  • 2022-12-23
  • 2021-12-27
相关资源
相似解决方案