Resource
Rails Generate Resource (rails g resource
) is the generator for a Rails resource. The Resource generator will create the model, controller, migration and associated tests (but will not create any views).
The Resource generator is similar to the scaffold_controller generator since they both create part of a REST-ful resource — the Resource generator creates the model and controller, whereas scaffold_controller creates the controller and views.
It's also related to the scaffold generator, which creates an entire REST-ful resource (models, controller and views).
As with all the model-based generators, you can pass quite sophisticated arguments for the model fields —
# username has a 30 character limit and a unique index
rails g resource User username:string{30}:uniq
# references supplier polymorphically with a standard index
rails g resource Product supplier:references{polymorphic}:index
rails g resource
rails g resource
Model Name
Model fields (as pairs)
Generator Options
Runtime Options
❯ rails g resource Usage: rails generate resource NAME [field[:type][:index] field[:type][:index]] [options] Options: [--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated engines) [--skip-collision-check], [--no-skip-collision-check] # Skip collision check [--force-plural], [--no-force-plural] # Forces the use of the given model name -o, --orm=NAME # ORM to be invoked # Default: active_record [--model-name=MODEL_NAME] # ModelName to be used -c, --resource-controller=NAME # Resource controller to be invoked # Default: controller -a, [--actions=ACTION ACTION] # Actions for the resource controller --resource-route # Indicates when to generate resource route # Default: true ActiveRecord options: [--migration], [--no-migration] # Indicates when to generate migration # Default: true [--timestamps], [--no-timestamps] # Indicates when to generate timestamps # Default: true [--parent=PARENT] # The parent class for the generated model [--indexes], [--no-indexes] # Add indexes for references and belongs_to columns # Default: true [--primary-key-type=PRIMARY_KEY_TYPE] # The type for primary key --db, [--database=DATABASE] # The database for your model's migration. By default, the current environment's primary database is used. -t, [--test-framework=NAME] # Test framework to be invoked # Default: Controller options: [--skip-routes], [--no-skip-routes] # Don't add routes to config/routes.rb. [--helper], [--no-helper] # Indicates when to generate helper # Default: true -e, [--template-engine=NAME] # Template engine to be invoked # Default: tailwindcss Runtime options: -f, [--force] # Overwrite files that already exist -p, [--pretend], [--no-pretend] # Run but do not make any changes -q, [--quiet], [--no-quiet] # Suppress status output -s, [--skip], [--no-skip] # Skip files that already exist
Description: Generates a new resource including an empty model and controller suitable for a RESTful, resource-oriented application. Pass the singular model name, either CamelCased or under_scored, as the first argument, and an optional list of attribute pairs. Attribute pairs are field:type arguments specifying the model's attributes. Timestamps are added by default, so you don't have to specify them by hand as 'created_at:datetime updated_at:datetime'. You don't have to think up every attribute up front, but it helps to sketch out a few so you can start working with the model immediately. This generator invokes your configured ORM and test framework, besides creating helpers and add routes to config/routes.rb. Unlike the scaffold generator, the resource generator does not create views or add any methods to the generated controller. Examples: `bin/rails generate resource post` # no attributes `bin/rails generate resource post title:string body:text published:boolean` `bin/rails generate resource purchase order_id:integer amount:decimal`