Generate a controller with all the RESTful functions

Ruby on-RailsControllerRestRspec

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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

Solution 4 - Ruby on-Rails

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.

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

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 :).

Solution 8 - Ruby on-Rails

script/generate rspec_scaffold Property

Solution 9 - Ruby on-Rails

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

Solution 10 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBarbView Question on Stackoverflow
Solution 1 - Ruby on-RailsMarcos PlaconaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsgdelfinoView Answer on Stackoverflow
Solution 3 - Ruby on-RailskonungView Answer on Stackoverflow
Solution 4 - Ruby on-RailspmargreffView Answer on Stackoverflow
Solution 5 - Ruby on-RailsVarus SeptimusView Answer on Stackoverflow
Solution 6 - Ruby on-RailsmolfView Answer on Stackoverflow
Solution 7 - Ruby on-RailsTom HammondView Answer on Stackoverflow
Solution 8 - Ruby on-RailsstephenmurdochView Answer on Stackoverflow
Solution 9 - Ruby on-RailsrfundukView Answer on Stackoverflow
Solution 10 - Ruby on-RailsphilippinedevView Answer on Stackoverflow