user3133810
user3133810

Reputation:

Generating a new Controller action in rails via command line

How can I add a new controller action through the command line so that rails automatically generates a view for the action?

Upvotes: 0

Views: 4847

Answers (1)

Alexander Kireyev
Alexander Kireyev

Reputation: 10825

See bin/rails generate chapter @ guides.rubyonrails.org

bin/rails generate controller CreditCards open debit credit close

# Credit card controller with URLs like /credit_cards/debit.
# Controller: app/controllers/credit_cards_controller.rb
# Test:       test/controllers/credit_cards_controller_test.rb
# Views:      app/views/credit_cards/debit.html.erb [...]
# Helper:     app/helpers/credit_cards_helper.rb

If you already have a controller, do the same, just skip overwriting other stuff.

From the link:

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    test_unit
   create      test/helpers/greetings_helper_test.rb
   invoke  assets
   invoke    scss
   create      app/assets/stylesheets/greetings.css.scss

As you can see it generates view for one action.

Upvotes: 2

Related Questions