Ruby-on-rails 当模型已经存在时,如何运行“rails generate scaffold”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4333393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:46:20  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3

提问by Lan

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 scaffoldto 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)

回答by Lee Jarvis

TL;DR: rails g scaffold_controller <name>

特尔;博士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 generateoption. If you run rails generate -hyou can see all of the options available to you.

即使您已经有一个模型,您仍然可以使用该rails generate选项生成必要的控制器和迁移文件。如果您运行,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. Just for clarity, here's the description on that:

如果您想为您的模型生成控制器脚手架,请参阅scaffold_controller。为了清楚起见,这里是关于它的描述:

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.

存根脚手架控制器及其视图。传递模型名称,CamelCased 或 under_scored,以及作为参数的视图列表。控制器名称被检索为模型名称的复数形式。

要在模块内创建控制器,请将模型名称指定为类似“parent_module/controller_name”的路径。

这会在 app/controllers 中生成一个控制器类并调用助手、模板引擎和测试框架生成器。

To create your resource, you'd use the resourcegenerator, and to create a migration, you can also see the migrationgenerator (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 scaffoldwith the --skipoption to skip any files which exist :)

要创建资源,您将使用resource生成器,而要创建迁移,您还可以看到migration生成器(看,所有这些疯狂都有一个模式)。这些提供了创建丢失文件以构建资源的选项。或者你可以只运行rails generate scaffold--skip选择跳过它存在的任何文件:)

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.

我建议花一些时间查看生成器内部的选项。我觉得它们在书籍等中没有得到很好的记录,但它们非常方便。

回答by tokhi

Great answer by Lee Jarvis, this is just the command e.g; we already have an existing model called User:

很好的回答Lee Jarvis,这只是命令,例如;我们已经有一个名为 User 的现有模型:

rails g scaffold_controller User

回答by frenesim

For the ones starting a rails app with existing database there is a cool gem called schema_to_scaffoldto generate a scaffold script. it outputs:

对于那些使用现有数据库启动 rails 应用程序的人,有一个很酷的 gem 被调用schema_to_scaffold来生成一个脚手架脚本。它输出:

rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string

rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string

from your schema.rbour your renamed schema.rb.Check it

从你的schema.rb我们的你改名schema.rb.检查一下

回答by Andrew Hendrie

This command should do the trick:

这个命令应该可以解决问题:

$ rails g scaffold movie --skip

回答by Nesha Zoric

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 脚手架

回答by Deepak Mahakale

You can make use of scaffold_controllerand remember to pass the attributesof 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