(仅)在创建控制器和模型后创建 Ruby on Rails 视图
我已经获得了一个具有控制器(仅最少代码)和模型的项目,但缺少视图。有没有办法仅使用脚手架或其他工具来生成视图?
I've obtained a project that have controllers (minimal code only) and models, but the views are missing. Is there a way to generate the views only using scaffold or another tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是脚手架生成器内部调用的内容:
erb
是使用的模板引擎,因此您也可以使用haml:scaffold
。您必须明确指定您希望脚手架使用的字段——rails不会自动从创建的模型中推断出它们。例如:
请参阅
rails g --help
了解跳过、强制覆盖和空运行等选项,或generatescaffold --help
了解特定于生成脚手架的信息。This is what the scaffold generator calls internally:
erb
is the templating engine used, so you can also usehaml:scaffold
.You must explicitly specify the fields you would like the scaffolding to use--rails does not automatically deduce them from the created model. For example:
See
rails g --help
for options like skipping, forcing overwriting, and dry runs orgenerate scaffold --help
for information specific to generating scaffolding.我刚刚遇到和你一样的问题。我做到了。更多详细信息如下:
- 首先,我将views/your_model文件夹重命名为views/your_model_bak。为了以后失败时恢复
- 然后,执行命令
-- 最后,您应该在 your_model 控制器中更新您的许可证。
希望它可以帮助你。
I just encounter the same your problem. I did it. More detail is below:
- First I rename views/your_model folder to views/your_model_bak. In order to revert if fail later
- Then, execute command
-- Finally, you should update your permit in your_model controller.
Hope it can help you.
“另一个工具”...
能够执行“
script/generate view_for model_name
”怎么样? :)有一个宝石可以做到这一点 - View Mapper。它有 Ruby on Rails 2 和 3 版本。
"Another tool"...
How about being able to do "
script/generate view_for model_name
"? :)There is a gem for that - View Mapper. It has Ruby on Rails 2 and 3 versions.
一个小技巧是如果使用 Rspec 并且不希望为规范/视图中的每个视图生成测试文件,请添加“
--no-test-framework
”One small tip is to add "
--no-test-framework
" if using Rspec and don't want test files generated for each view in spec/views要在创建控制器和模型后生成视图,您可以使用命令行。您切换到要在其中创建新视图的文件夹。例如:
要返回一个目录,请使用:
To generate views after controller and models are already created, you may use the command line. You switch to the folder in which you want to create the new view. For example:
To go back of one directory use:
--skip
表示跳过已经存在的文件。 (相反的是--force
。)如果您不需要助手,
--helpers=false
。删除我的
User
视图后的示例输出:The
--skip
means to skip files that already exist. (The opposite is--force
.)If you don't want helpers,
--helpers=false
.Sample output after deleting my
User
views: