[英]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.我是 Rails 的新手,所以我当前的项目处于一种奇怪的状态。
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.我现在意识到我应该用
rails generate scaffold
生成它来连接路由、视图、控制器等。
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)
(使用导轨 3)
TL;DR : rails g scaffold_controller <name>
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.即使您已经有了一个模型,您仍然可以使用
rails generate
选项生成必要的控制器和迁移文件。 If you run rails generate -h
you can see all of the options available to you.如果您运行
rails generate -h
您可以看到所有可用的选项。
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
.如果您想为模型生成控制器脚手架,请参阅
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.
传递模型名称,CamelCased 或 under_scored,以及作为参数的视图列表。 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'.
要在模块内创建控制器,请将模型名称指定为类似“parent_module/controller_name”的路径。
This generates a controller class in app/controllers and invokes helper, template engine and test framework generators.
这会在 app/controllers 中生成一个控制器类并调用助手、模板引擎和测试框架生成器。
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).要创建您的资源,您将使用
resource
生成器,而要创建迁移,您还可以看到migration
生成器(看,所有这些疯狂都有一个模式)。 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 :)或者,您可以使用
--skip
选项运行rails generate scaffold
以跳过任何存在的文件:)
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.
我觉得它们在书籍等中没有得到很好的记录,但它们非常方便。
Great answer by Lee Jarvis
, this is just the command eg; Lee Jarvis
回答很好,这只是命令,例如; we already have an existing model called User:我们已经有一个名为 User 的现有模型:
rails g scaffold_controller User
For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffold
to generate a scaffold script.对于使用现有数据库启动 rails 应用程序的人,有一个很酷的 gem,称为
schema_to_scaffold
来生成脚手架脚本。 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.
从您的
schema.rb
我们您重命名的schema.rb.
Check it核实
In Rails 5 , you can still run在Rails 5 中,您仍然可以运行
$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如需更好的解释,请查看Rails 脚手架
这个命令应该可以解决问题:
$ rails g scaffold movie --skip
You can make use of scaffold_controller
and remember to pass the attributes
of the model, or scaffold will be generated without the attributes.可以使用
scaffold_controller
记得传递模型的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
I had this challenge when working on a Rails 6 API application in Ubuntu 20.04 .我在Ubuntu 20.04 中处理Rails 6 API 应用程序时遇到了这个挑战。
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 .我已经有模型,我需要为模型生成相应的控制器,并在控制器 params 中添加它们允许的属性。
Here's how I did it :这是我如何做到的:
I used the rails generate scaffold_controller
to get it done.我使用
rails generate scaffold_controller
来完成它。
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我希望这有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.