Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: How to run `rails generate scaffold` when the model already exists?

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)

like image 735
Lan Avatar asked Dec 02 '10 09:12

Lan


People also ask

How do you generate scaffold in rails?

To generate a fully working scaffold for a new object, including model, controller, views, assets, and tests, use the rails g scaffold command. Then you can run rake db:migrate to set up the database table. Then you can visit http://localhost:3000/widgets and you'll see a fully functional CRUD scaffold.

What is a scaffold command?

One of the products of the rails generate scaffold command is a database migration. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database.

How do you remove scaffold rails?

You can remove scaffold in the following way: Generate scaffold: $rails generate scaffold Story. If you migrated your files, perform a rollback: $rake db:rollback. Destroy or undo scaffold: $rails destroy scaffold Story.

What is scaffold command in Ruby?

Scaffolding in Ruby on Rails refers to the auto generation of a simple set of a model, views and controller usually for a single table. For example: user@localhost$ ./scripts/generate scaffold users. Would create a full CRUD (create, read, update, delete) web interface for the Users table.


2 Answers

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.

like image 175
Lee Jarvis Avatar answered Oct 06 '22 02:10

Lee Jarvis


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 
like image 43
tokhi Avatar answered Oct 06 '22 03:10

tokhi