0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
twinturbotom
  • 1,504
  • 1
  • 21
  • 34

1 Answers1

1

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
Ian Armit
  • 673
  • 5
  • 10
  • Controller has already been created... I would think re-running this script would overwrite whats in it but I'll verify. Thanks. – twinturbotom Oct 30 '12 at 01:02
  • If the controller is made it prompts you with what you want to do I think. – Ian Armit Oct 30 '12 at 03:14
  • that command overwrites existing controllers... Looking to simply append the action/method, create the html.erb, and append routes/ Thanks. – twinturbotom Oct 30 '12 at 13:05
  • Then I think your only option to do this is to write your own generator. http://guides.rubyonrails.org/generators.html – Ian Armit Oct 30 '12 at 14:11