Ruby on Rails Rails generate commands Rails Generate Controller

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

we can create a new controller with rails g controller command.

$ bin/rails generate controller controller_name

The controller generator is expecting parameters in the form of generate controller ControllerName action1 action2.

The following creates a Greetings controller with an action of hello.

$ bin/rails generate controller Greetings hello

You will see the following output

     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

This generates the following

FileExample
Controller Filegreetings_controller.rb
View Filehello.html.erb
Functional Test Filegreetings_controller_test.rb
View Helpergreetings_helper.rb
JavaScript Filegreetings.coffee

It will also add routes for each action in routes.rb



Got any Ruby on Rails Question?