当模型已经存在时,如何运行“rails生成脚手架”?

时间:2022-11-24 09:18:06

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生成脚手架来生成它,以连接路由、视图、控制器等。

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)

对我来说,为我的“电影”搭建脚手架的最好方法是什么?(使用rails 3)

6 个解决方案

#1


547  

TL;DR: rails g scaffold_controller <name>

rails g搭建物控制器 <名称> 。

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 generate选项生成必要的控制器和迁移文件。如果运行rails生成-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 - d_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.

存根化一个脚手架的控制器及其视图。传递模型名称,无论是camel或under_score,还是作为参数的视图列表。控制器名作为模型名的多元版本检索。

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/controller中生成一个控制器类,并调用助手、模板引擎和测试框架生成器。

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

要创建您的资源,您需要使用资源生成器,并且要创建迁移,您还可以看到迁移生成器(请参见,所有这些疯狂行为都有一个模式)。这些选项提供了创建缺失文件以构建资源的选项。或者,您可以使用-skip选项运行rails生成脚手架,以跳过存在的任何文件:)

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.

我建议花点时间看看生成器内部的选项。我不觉得这些东西在书里有很好的记载,但是它们很方便。

#2


60  

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

很好的回答,这就是命令;我们已经有一个叫做User的现有模型:

rails g scaffold_controller User

#3


28  

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应用程序的用户,有一个叫做schema_to_scaffold的很酷的gem来生成一个scaffold脚本。输出:

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

rails g scaffold用户fname:string lname:string lname:string bdate:date email:string encrypted_password:string

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

从你的模式。我们重新命名的模式。检查它

#4


12  

This command should do the trick:

这个命令应该具有以下功能:

$ rails g scaffold movie --skip

#5


7  

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 old_controller并记住传递模型的属性,或者生成没有属性的scaffold。

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

#6


3  

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

#1


547  

TL;DR: rails g scaffold_controller <name>

rails g搭建物控制器 <名称> 。

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 generate选项生成必要的控制器和迁移文件。如果运行rails生成-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 - d_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.

存根化一个脚手架的控制器及其视图。传递模型名称,无论是camel或under_score,还是作为参数的视图列表。控制器名作为模型名的多元版本检索。

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/controller中生成一个控制器类,并调用助手、模板引擎和测试框架生成器。

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

要创建您的资源,您需要使用资源生成器,并且要创建迁移,您还可以看到迁移生成器(请参见,所有这些疯狂行为都有一个模式)。这些选项提供了创建缺失文件以构建资源的选项。或者,您可以使用-skip选项运行rails生成脚手架,以跳过存在的任何文件:)

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.

我建议花点时间看看生成器内部的选项。我不觉得这些东西在书里有很好的记载,但是它们很方便。

#2


60  

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

很好的回答,这就是命令;我们已经有一个叫做User的现有模型:

rails g scaffold_controller User

#3


28  

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应用程序的用户,有一个叫做schema_to_scaffold的很酷的gem来生成一个scaffold脚本。输出:

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

rails g scaffold用户fname:string lname:string lname:string bdate:date email:string encrypted_password:string

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

从你的模式。我们重新命名的模式。检查它

#4


12  

This command should do the trick:

这个命令应该具有以下功能:

$ rails g scaffold movie --skip

#5


7  

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 old_controller并记住传递模型的属性,或者生成没有属性的scaffold。

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

#6


3  

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