ChatGPT解决这个技术问题 Extra ChatGPT

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)

It's probably best to write your own controllers and views and routes. You'll learn more about the concepts
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.
Not directly relevant but if you ever mess up, use rails destroy . I remember this was really useful when I started.
@BenAubin, while true, the beauty of Rails is to not have to continually write boilerplate.

L
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.


wow, one of the best answers i've ever gotten to a programming question. thanks!
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 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 - 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... stackoverflow.com/q/17153864/165673
t
tokhi

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

Thank you, for those of us too lazy to read paragraphs of text!
S
SierraOscar

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


This is a great litle gem, it will help me a lot! Should be built-in to Rails!
N
Nesha Zoric

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


A
Andrew Hendrie

This command should do the trick:

$ rails g scaffold movie --skip

D
Deepak Mahakale

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

P
Promise Preston

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


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now