How can I add a new controller action through the command line so that rails automatically generates a view for the action?
1 Answer
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.
2 Comments
silva96
not valid in rails 6 $ rails g controller Users invite returns: The name 'UsersController' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative or use --skip-collision-check or --force to skip this check and run this generator again.
Vayl
Interesting. Do you know if it's possible to only create actions for a controller that already exists?