Rails:当模型已经存在时如何运行“rails generate scaffold"? [英] Rails: How to run `rails generate scaffold` when the model already exists?
问题描述
我是 Rails 的新手,所以我当前的项目处于一种奇怪的状态.
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.
我现在意识到我应该用 rails generate scaffold
生成它来连接路由、视图、控制器等.
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.
现在为我的电影"创建脚手架的最佳方式是什么?(使用导轨 3)
What's the best way for me to create scaffolding for my "Movie" now? (using rails 3)
推荐答案
TL;DR: rails g scaffold_controller
即使您已经有了一个模型,您仍然可以使用 rails generate
选项生成必要的控制器和迁移文件.如果您运行 rails generate -h
,您可以看到所有可用的选项.
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
如果您想为模型生成控制器脚手架,请参阅scaffold_controller
.为清楚起见,以下是相关说明:
If you'd like to generate a controller scaffold for your model, see scaffold_controller
. Just for clarity, here's the description on that:
存根脚手架控制器及其视图.传递模型名称,CamelCased 或 under_scored,以及作为参数的视图列表.控制器名称作为模型的复数版本被检索名字.
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.
要在模块中创建控制器,请将模型名称指定为路径如parent_module/controller_name".
To create a controller within a module, specify the model name as a path like 'parent_module/controller_name'.
这会在 app/controllers 中生成一个控制器类并调用 helper,模板引擎和测试框架生成器.
This generates a controller class in app/controllers and invokes helper, template engine and test framework generators.
要创建资源,您将使用 resource
生成器,要创建迁移,您还可以查看 migration
生成器(请参阅,有一个模式所有这些疯狂).这些提供了创建丢失文件以构建资源的选项.或者,您可以使用 --skip
选项运行 rails generate scaffold
以跳过任何存在的文件:)
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.
这篇关于Rails:当模型已经存在时如何运行“rails generate scaffold"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!