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

Lan

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)

Lee Jarvis

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ruby on Rails - model from generate scaffold command

NoMethodError when using "rails generate scaffold"

Generate scaffold for multiple namespaces using same migration and model on rails 6

rails scaffold not plural generate

Rails: How to show if value already exists in database

Error when trying to Scaffold a model

How to check if an item is already in a model table in Rails

ElasticSearch Rails resource_already_exists_exception when running tests

How to not run a python exe if it already exists in task manager

In rails, how do I run all my model tests when I run bin/rspec?

Why is model's clean run when viewing an already submitted form?

How to generate a model for a mongoengine Document from an already existing collection

How to generate a model for a mongoengine Document from an already existing collection

Generate scaffold which references a model who refers another model

How to insert data to tables which is in "has and belongs to many" relationship. Controller and Model generated by scaffold in rails

How to implement a class of sr-only when a class already exists

How to check if an item already exists in table when inserting from a XML

how to create a pull request on bitbucket when local branch already exists

How to overwrite a folder if it already exists when creating it with makedirs?

How to overwrite a folder if it already exists when creating it with makedirs?

How to update a map but only when key already exists

How to check if mail already exists in database when using updateProfile () function

How to suppress "File already exists" prompt when overwriting?

How to generate spec with scaffold.Rspec

How to load an already trained XGBoost model to run on a new dataset?

"rails generate model" hangs

Can EFCore Scaffold-DbContext generate custom Model names?

'rails generate' commands hang when trying to create a model

Brand new rails app, can't generate scaffold