364

I'm new to Rails so my current project is in a weird state.

One of the first things I generated was a "Movie" model. I then started defining it in more detail, added a few methods, etc.

I now realize I should have generated it with rails generate scaffold to hook up things like the routing, views, controller, etc.

I tried to generate the scaffolding but I got an error saying a migration file with the same name already exists.

What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Lan
  • 6,039
  • 7
  • 22
  • 24
  • 1
    It's probably best to write your own controllers and views and routes. You'll learn more about the concepts – Ben Aubin Mar 13 '15 at 17:07
  • 1
    Agree with @penne12. At least in the beginning, until you are comfortable with the concepts, so that you know what all the code is doing that is generated for you. – mydoghasworms Mar 23 '15 at 07:35
  • 1
    Not directly relevant but if you ever mess up, use rails destroy . I remember this was really useful when I started. – Noel Dec 27 '15 at 20:21
  • 2
    @BenAubin, while true, the beauty of Rails is to not have to continually write boilerplate. – Romuloux Jul 19 '17 at 18:02

7 Answers7

654

TL;DR: rails g scaffold_controller <name>

Even though you already have a model, you can still generate the necessary controller and migration files by using the rails generate option. If you run rails generate -h you can see all of the options available to you.

Rails:
  controller
  generator
  helper
  integration_test
  mailer
  migration
  model
  observer
  performance_test
  plugin
  resource
  scaffold
  scaffold_controller
  session_migration
  stylesheets

If you'd like to generate a controller scaffold for your model, see scaffold_controller. Just for clarity, here's the description on that:

Stubs out a scaffolded controller and its views. Pass the model name, either CamelCased or under_scored, and a list of views as arguments. The controller name is retrieved as a pluralized version of the model name.

To create a controller within a module, specify the model name as a path like 'parent_module/controller_name'.

This generates a controller class in app/controllers and invokes helper, template engine and test framework generators.

To create your resource, you'd use the resource generator, and to create a migration, you can also see the migration generator (see, there's a pattern to all of this madness). These provide options to create the missing files to build a resource. Alternatively you can just run rails generate scaffold with the --skip option to skip any files which exist :)

I recommend spending some time looking at the options inside of the generators. They're something I don't feel are documented extremely well in books and such, but they're very handy.

Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • 12
    wow, one of the best answers i've ever gotten to a programming question. thanks! – Lan Dec 02 '10 at 09:45
  • 12
    I also have existing models and doing `rails generate scaffold_controller MyModel` does generate the view layer but it doesn't include the model attributes (table columns) and you have to add those by hand. Anybody know of a fix for this? I am on Rails 3.2.8. – aaronbartell Dec 03 '12 at 22:10
  • @aaronbartell You're asking Rails to generate a 'scaffold_controller' which doesn't care about model attributes. This is not broken, if you want a model generated too then you want `generate scaffold` if you have a model but you want a scaffold controller and new attributes added to a model you want `scaffold_generator` followed by `migration` with the fields you're altering – Lee Jarvis Dec 04 '12 at 11:34
  • 9
    @Lee - No aaronbartell is asking how come the scaffold_controller doesn't generate the corresponding view inputs for the existing attributes of the model that was passed to it, which is a valid question... http://stackoverflow.com/q/17153864/165673 – Yarin Jun 17 '13 at 22:49
  • This frankly is not very useful when the majority of the effort of scaffolding is building the view files, and the view files generated by `scaffold_controller` are incomplete. – michael_teter Sep 16 '22 at 21:03
76

Great answer by Lee Jarvis, this is just the command e.g; we already have an existing model called User:

rails g scaffold_controller User
tokhi
  • 21,044
  • 23
  • 95
  • 105
33

For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffold to generate a scaffold script. it outputs:

rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string

from your schema.rb our your renamed schema.rb. Check it

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
frenesim
  • 703
  • 9
  • 10
15

In Rails 5, you can still run

$rails generate scaffold movie --skip

to create all the missing scaffold files or

rails generate scaffold_controller Movie

to create the controller and view only.

For a better explanation check out rails scaffold

Nesha Zoric
  • 6,218
  • 42
  • 34
12

This command should do the trick:

$ rails g scaffold movie --skip
Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71
11

You can make use of scaffold_controller and remember to pass the attributes of the model, or scaffold will be generated without the attributes.

rails g scaffold_controller User name email
# or
rails g scaffold_controller User name:string email:string

This command will generate following files:

create  app/controllers/users_controller.rb
invoke  haml
create    app/views/users
create    app/views/users/index.html.haml
create    app/views/users/edit.html.haml
create    app/views/users/show.html.haml
create    app/views/users/new.html.haml
create    app/views/users/_form.html.haml
invoke  test_unit
create    test/controllers/users_controller_test.rb
invoke  helper
create    app/helpers/users_helper.rb
invoke    test_unit
invoke  jbuilder
create    app/views/users/index.json.jbuilder
create    app/views/users/show.json.jbuilder
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
5

I had this challenge when working on a Rails 6 API application in Ubuntu 20.04.

I had already existing models, and I needed to generate corresponding controllers for the models and also add their allowed attributes in the controller params.

Here's how I did it:

I used the rails generate scaffold_controller to get it done.

I simply ran the following commands:

rails generate scaffold_controller School name:string logo:json motto:text address:text

rails generate scaffold_controller Program name:string logo:json school:references

This generated the corresponding controllers for the models and also added their allowed attributes in the controller params, including the foreign key attributes.

create  app/controllers/schools_controller.rb
invoke  test_unit
create    test/controllers/schools_controller_test.rb

create  app/controllers/programs_controller.rb
invoke  test_unit
create    test/controllers/programs_controller_test.rb

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143