93

I am trying to generate a controller with all the RESTful actions stubbed. I had read at Wikibooks - Ruby on Rails that all I needed to do was to call the generator with the controller name and I would get just that. So, I ran script/generate rspec_controller Properties but got an empty controller.

Any other suggestions would be greatly appreciated.

1
  • 1
    thenduks, If I was as experienced as you that would be true, but I don't even know the 7 restful actions off the top of my head so I would have to google the item and then switch back and forth between the page and my app to enter each one. Once I know them, I agree that your solution is easier
    – Barb
    Mar 23, 2010 at 22:53

10 Answers 10

153

I don't know about an automated way of doing it, but if you do:

script/generate controller your_model_name_in_plural new create update edit destroy index show

All of them will be created for you

Update for Rails 4

rails g scaffold_controller Property
5
  • 7
    Great Answer. Experts can ignore the rest. Note to noobs - if you name your controller PropertyController as I misunderstood :) you will get PropertyController Controller :)
    – Barb
    Mar 23, 2010 at 23:07
  • 2
    My answer does exactly the same thing - except you don't need to specify all of the default 7 actions.
    – konung
    Mar 24, 2010 at 1:14
  • 4
    Just a note... this only generates a controller with methods named for basic CRUD actions. The routes are far from RESTful... e.g. route get "mycontroller/create" (create should be a post)
    – rthbound
    Oct 29, 2012 at 16:32
  • I would agree that this answer is more suited for generic custom actions, it does not actually respect the RESTful convention which is desired.
    – prusswan
    Jul 8, 2013 at 10:01
  • This answer is outdated for rails 4. See konung's answer for Rails 4.
    – toobulkeh
    Sep 23, 2015 at 2:10
97

In Rails 3 there is also rails generate scaffold_controller .... More info here.

1
  • 1
    I prefer this method as it generates RESTful routes. Using rails g controller Foos create would generate a route for get '/create' which is not RESTful at all.
    – rthbound
    Oct 29, 2012 at 16:28
39

EDIT(due to some comments) : Original question was in 2010 - hence the answer is NOT for RAILS 4 , but for rails 2!!

try using scaffolding.

script/generate scaffold controller Properties

Section of Official docs on Ruby On Rails

I'm sure you can find more info if you do a google search on rails scaffolding. Hope that helps.

EDIT: For RAILS 4

rails g scaffold_controller Property

6
  • can you script/generate rspec_scaffold controller Properties?
    – Barb
    Mar 23, 2010 at 22:57
  • yes you could. Here is detailed info/tutorial: blog.davidchelimsky.net/2007/05/14/…
    – konung
    Mar 24, 2010 at 1:17
  • I suspect this answer is outdated for recent versions of Rails. It will simply create an entire scaffold for the object/model called "controller"
    – prusswan
    Jul 8, 2013 at 10:09
  • 4
    Yes that was written in 2010 for version 2.something as is follows from question. Now (Rails 4) it would look like this: rails g scaffold_controller Property ( scaffold_controller would invoke scaffolding and you need provide model name ( singular), but don't have to)
    – konung
    Jul 8, 2013 at 17:28
  • 2
    I can verify that rails g scaffold_controller Property works in rails 3.2 as well. This will create a PropertiesController with ALL 7 default RESTful actions and their respective views, without a model.
    – mkralla11
    Aug 29, 2013 at 13:31
17

In Rails 4/5 the following command does the trick for me.

rails g scaffold_controller Property --skip-template-engine

It generated the controller actions but not the view.

12

Rails 5.1

Starting point:

You have created a model without a controller, nor views (eg thru: rails generate model category)

Objective:

Upgrade it to a full RESTful resource

Command:

rails generate scaffold_controller category

It stubs out a scaffolded controller, its seven RESTful actions and related views. (Note: You can either pass the model name CamelCased or under_scored.)

Output:

varus@septimusSrv16DEV4:~/railsapps/dblirish$ rails generate scaffold_controller category
Running via Spring preloader in process 45681
      create  app/controllers/categories_controller.rb
      invoke  erb
      create    app/views/categories
      create    app/views/categories/index.html.erb
      create    app/views/categories/edit.html.erb
      create    app/views/categories/show.html.erb
      create    app/views/categories/new.html.erb
      create    app/views/categories/_form.html.erb
      invoke  test_unit
      create    test/controllers/categories_controller_test.rb
      invoke  helper
      create    app/helpers/categories_helper.rb
      invoke    test_unit
      invoke  jbuilder
      create    app/views/categories/index.json.jbuilder
      create    app/views/categories/show.json.jbuilder
      create    app/views/categories/_category.json.jbuilder
7

You're looking for scaffolding.

Try:

script/generate scaffold Property

This will give you a controller, a model, a migration and related tests. You can skip the migration with the option --skip-migration. If you don't want the others, you'll have to delete them yourself. Don't worry about overwriting existing files, that won't happen unless you use --force.

As klew points out in the comments, this also defines the method bodies for you, not just the names. It is very helpful to use as a starting point for your REST controller.

1
  • @Barb, I think Scaffolding would be better for you since it not only declares all functions but it also defines them. It is good to create them at least once and have them as an example.
    – klew
    Mar 24, 2010 at 9:33
6

In Rails 4 it's rails g controller apps new create update edit destroy show index

Or rails generate controller apps new create update edit destroy show index if you want to write out the full term :).

2
  • And for controllers with a longer name (like line_items), rails g controller LineItems
    – user3720516
    Mar 20, 2015 at 20:58
  • 1
    You can just use rails g scaffold_controller apps in Rails 4
    – wrdevos
    Sep 14, 2015 at 22:43
2

script/generate rspec_scaffold Property

1
  • and yes, I realise that you have already accepted an answer on this one, but I feel this solution might help some people who read this post out too.... the solution above will create a model etc, which you can just delete if you don't need Mar 24, 2010 at 10:31
0

There's no way (that I know of? that is documented?) to stub out a controller except through scaffolding. But you could do:

script/generate controller WhateverController new create edit update destroy show
1
  • Great answer, I think you are missing an action are there not 7 actions?
    – Barb
    Mar 23, 2010 at 23:01
0

One solution is to create a script that accepts one parameter, the controller name, and let the script type the whole command for you.


  1. Create a new file, say, railsgcontroller
  2. Make it executable and save it on path
  3. Run it like: $ railsgcontroller Articles

die () {
    echo "Please supply new rails controller name to generate."
    echo >&2 "$@"
    exit 1
}

[ "$#" -eq 1 ] || die "1 argument required, $# provided"

rails g controller "$1" new create update edit destroy show index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.