rails generate script for adding new pages?

2.9k views Asked by At

Creating a new page (within an existing controller) in rails is an easy 3 step process; any way of automating this? Creating a generate script that does the following maybe:

  1. add method to controller:

    def newPage end

  2. add route(s)

    get 'controller/newPage' match 'url' => 'controller#newPage'

  3. Create newPage.html.erb in the appropriate directory

I'd much rather automate this with a command like:

rails g controller:add newPage

I'm not really sure where to start.

1

There are 1 answers

4
Ian Armit On

If you use the command

rails generate controller controller_name page_name

it will create

  1. The controller if it's not made already, and the method inside it.
  2. The appropriate get statement in routes.rb
  3. Generate the view page in the controller's view directory (app/views/controller_name/page_name.html.erb)
  4. Appropriate tests/helper/css/javascripts