Ruby-on-rails 生成具有所有 RESTful 功能的控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2504123/
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
Generate a controller with all the RESTful functions
提问by Barb
I am trying to generate a controller with all the RESTful actions stubbed. I had read at link textthat all I needed to do was to use call the generator with just a controller name I would get just that. So, I ran "script/generate rspec_controller Properties" and I got an empty controller.
我正在尝试生成一个带有所有 RESTful 操作的控制器。我在链接文本中读到,我需要做的就是使用仅使用控制器名称调用生成器,我会得到的。所以,我运行了“脚本/生成 rspec_controller 属性”,我得到了一个空控制器。
Any other suggestions would be greatly appreciated.
任何其他建议将不胜感激。
回答by Marcos Placona
I don't know about an automated way of doing it, but if you do:
我不知道这样做的自动化方式,但如果你这样做:
script/generate controller your_model_name_in_plural new create update edit destroy index show
All of them will be created for you
所有这些都将为您创建
Update for Rails 4
Rails 4 的更新
rails g scaffold_controller Property
回答by gdelfino
回答by konung
EDIT(due to some comments): Original question was in 2010 - hence the answer is NOT for RAILS 4 , but for rails 2!!
编辑(由于一些评论):最初的问题是在 2010 年 - 因此答案不是针对 RAILS 4 ,而是针对 rails 2 !
try using scaffolding.
尝试使用脚手架。
script/generate scaffold controller Properties
Section of Official docs on Ruby On Rails
I'm sure you can find more info if you do a google search on rails scaffolding. Hope that helps.
如果您在谷歌搜索 Rails 脚手架,我相信您可以找到更多信息。希望有帮助。
EDIT:For RAILS 4
编辑:对于导轨 4
rails g scaffold_controller Property
rails g scaffold_controller Property
回答by pmargreff
In Rails 4/5 the following command does the trick for me.
在 Rails 4/5 中,以下命令对我有用。
rails g scaffold_controller Property --skip-template-engine
It generated the controller actions but not the view.
它生成了控制器动作而不是视图。
回答by Varus Septimus
Rails 5.1
导轨 5.1
Starting point:
初始点:
You have created a model without a controller, nor views (eg thru: rails generate model category)
您创建了一个没有控制器的模型,也没有视图(例如 thru: rails generate model category)
Objective:
客观的:
Upgrade it to a full RESTful resource
将其升级为完整的 RESTful 资源
Command:
命令:
rails generate scaffold_controller category
rails generate scaffold_controller category
It stubs out a scaffolded controller, its seven RESTful actions and related views. (Note: You can either pass the model name CamelCased or under_scored.)
它生成了一个脚手架控制器、七个 RESTful 操作和相关视图。(注意:您可以传递模型名称 CamelCased 或 under_scored。)
Output:
输出:
varus@septimusSrv16DEV4:~/railsapps/dblirish$ rails generate scaffold_controller category
Running via Spring preloader in process 45681
create app/controllers/categories_controller.rb
invoke erb
create app/views/categories
create app/views/categories/index.html.erb
create app/views/categories/edit.html.erb
create app/views/categories/show.html.erb
create app/views/categories/new.html.erb
create app/views/categories/_form.html.erb
invoke test_unit
create test/controllers/categories_controller_test.rb
invoke helper
create app/helpers/categories_helper.rb
invoke test_unit
invoke jbuilder
create app/views/categories/index.json.jbuilder
create app/views/categories/show.json.jbuilder
create app/views/categories/_category.json.jbuilder
回答by molf
You're looking for scaffolding.
您正在寻找脚手架。
Try:
尝试:
script/generate scaffold Property
This will give you a controller, a model, a migration and related tests. You can skip the migration with the option --skip-migration. If you don't want the others, you'll have to delete them yourself. Don't worry about overwriting existing files, that won't happen unless you use --force.
这将为您提供控制器、模型、迁移和相关测试。您可以使用选项跳过迁移--skip-migration。如果您不想要其他人,则必须自己删除它们。不要担心覆盖现有文件,除非您使用--force.
As klew points out in the comments, this also defines the method bodiesfor you, not just the names. It is very helpful to use as a starting point for your REST controller.
正如 klew 在评论中指出的那样,这也为您定义了方法主体,而不仅仅是名称。用作 REST 控制器的起点非常有帮助。
回答by Tom Hammond
In Rails 4 it's rails g controller apps new create update edit destroy show index
在 Rails 4 中 rails g controller apps new create update edit destroy show index
Or rails generate controller apps new create update edit destroy show indexif you want to write out the full term :).
或者rails generate controller apps new create update edit destroy show index如果你想写出完整的术语:)。
回答by stephenmurdoch
script/generate rspec_scaffold Property
脚本/生成 rspec_scaffold 属性
回答by rfunduk
There's no way (that I know of? that is documented?) to stub out a controller except through scaffolding. But you could do:
除了通过脚手架之外,没有办法(我知道的?有记录的?)来消除控制器。但你可以这样做:
script/generate controller WhateverController new create edit update destroy show
回答by philippinedev
One solution is to create a script that accepts one parameter, the controller name, and let the script type the whole command for you.
一种解决方案是创建一个脚本,该脚本接受一个参数,即控制器名称,并让脚本为您键入整个命令。
- Create a new file, say, railsgcontroller
- Make it executable and save it on path
- Run it like: $ railsgcontroller Articles
- 创建一个新文件,比如 railsgcontroller
- 使其可执行并将其保存在路径上
- 像这样运行: $ railsgcontroller 文章
die () {
echo "Please supply new rails controller name to generate."
echo >&2 "$@"
exit 1
}
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
rails g controller "" new create update edit destroy show index

