Ruby on Rails 生成视图
有没有办法使用railsgenerate命令单独生成视图?我也愿意安装一个 gem 来完成现有的任务。基本上,脚手架命令给了我太多的东西,我宁愿手动编写我的控制器。但是,使用记录表编写索引视图的效率不是很高。
Is there a way to generate the views separately using the rails generate command? I would also be willing to install a gem to accomplish that task f one exists. Basically the scaffolding command gives me too much and I would rather code my controller by hand. However, writing the index view with a table for the records would not be very efficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用控制器生成器生成控制器和视图。
这将创建操作
new
和create
及其相应的视图。您仍然需要手动设置路线。
You can generate the controller and the view using the controller generator.
This will create actions
new
andcreate
with their corresponding views.You still need to set up your routes manually with this.
一种特殊情况是当您想要向现有控制器添加新视图时。
在这种情况下,只需使用常规命令,但每次提示时都要小心说
'n'
,以免覆盖现有文件。例如,将名为
'invite'
的视图添加到名为'projects'
的现有控制器:One particular situation is when you want to add a new view to an existing controller.
In that case, just use the regular command, but be careful to say
'n'
every time prompted in order to not overwrite existing files.For example, adding a view called
'invite'
to an existing controller named'projects'
:正如 Sameers 之前提到的,有一篇文章展示了如何生成视图。它将使用 Rails 默认模板为您的模型创建所有视图,这非常方便。
如果像我一样你想要一些更可定制的东西,你可以实现以下目标。
您可以创建自己的生成器,这样您就拥有了这样的东西。
Rails 生成视图 NAME VIEW [选项]
要实现此目的,您需要执行以下操作。
railsgenerategeneratorview
这将在 lib/generators/view/ 文件夹中为您生成一些文件。
打开 view_generator.rb 文件并添加以下代码。
接下来,您需要创建我们使用在generate_view方法中使用的实际模板的文件。
以操作 new 为例,创建一个 filelib/generators/view/new.html.erb 并添加以下内容。
根据需要自定义模板视图。您还需要添加 _form.html.erb。在 view_generator.rb 文件中添加任何其他变量和逻辑即可完成。
这需要更多的工作,但如果您发现自己一直生成类似的视图,那么这可能是值得的。
我能想到的这种方法的最佳用例是,如果您对平台进行白标并且需要为客户配置文件生成多个文件。
As previously mentioned by sameers there was post that showed how to just generate a the views. It will create all the views for your model using the rails default templates which is very handy.
If like me you want something a little more customizable you can achieve the following.
You can create your own generator so you have something like this.
rails generate view NAME VIEW [options]
To achieve this you need to do the following.
rails generate generator view
This will generate a few files for you in lib/generators/view/ folder.
Open the view_generator.rb file and add the following code.
Next you need to create the file that we are using actual template used in generate_view method.
Using the action new as example, create a filelib/generators/view/new.html.erb and add the following.
Customize the template view as much as you want. You will need to add the _form.html.erb as well. Add any additional variables and logic in your view_generator.rb file and you are done.
It's more work but can be worth it if you find yourself generating similar views all the time.
Best use case I can think of for this approach is if you white label your platform and need to generate multiple files for a clients profile.
第一部分是模型/控制器的名称,第二部分是操作。
the first part is the name of the model/controller, the second part are the actions.
我创建了一个 https://github.com/coezbek/rails_view_generator ,其目的只是创建脚手架视图,而不需要脚手架控制器或模型。
I have created a
https://github.com/coezbek/rails_view_generator
for the purpose of only creating the scaffold views, without the scaffold controller or model.