我正在为此寻找一个好的 Rails 资源
我想要一个关于如何设置控制器操作和表单以在其所属的另一个资源的视图中创建资源的良好来源...
I'd like a good source on how to set up controller actions and forms for creating a resource inside the view of another resource that it belongs_to...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像平常一样设置控制器。您需要使用 Rails 的嵌套属性功能。这使您能够使用一种形式在创建子对象的同时创建其父对象。
这是我的首选链接对于嵌套属性。如果您运行的是 Ruby 1.9.2,则唯一需要进行的更改是在
setup_person
帮助程序中。returning
已被弃用,因此您可以将其更改为:在典型的 Rails 风格中,这将正常工作,为每个资源使用标准控制器。
其他链接
http://weblog.rubyonrails.org/2009/1/26 /nested-model-forms
http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/
Set up your controllers as you would normally. You'll need to use the nested attributes feature of Rails. This enables you to create children objects at the same time as creating their parent using one form.
This is my go-to link for nested attributes. The only change you will need to make if you are running Ruby 1.9.2 is in the
setup_person
helper.returning
has been deprecated so you can change it to:In typical Rails style, this will just work using standard controllers for each of your resources.
Other links
http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/
我没有一个网络资源来记录我通常所做的事情,但我创建了一个 gist 来记录我所做的事情我最常在这里这样做: https://gist.github.com/900241
要点的前提是您有一个包含许多项目角色的项目模型,并且您想要在项目表单中编辑许多项目角色。这几乎是经典的accepts_nested_attributes_for场景,几乎任何谈论它的页面都会给你一个不错的文章。问题是,我见过的解决方案总是涉及一些严重混乱的干扰性 JavaScript,它们会逃脱整个表单视图并将其放入链接的 onClick 方法中。我最近想出了一种使用 jQuery 模板的更干净、不引人注目的方法。
当您移动到嵌套模型时,您不必对 ProjectsController 执行任何操作。一切都在控制器级别运行,您甚至不需要 ProjectRolesController。 (这就是为什么我没有费心将它们包含在要点中。)在模型级别,它只是标准的
accepts_nested_attributes_for
。有趣的地方在于视图。项目表单有两个 form_for 块:一个呈现 jQuery 模板,另一个呈现项目角色表单。 jQuery 模板反过来仅呈现项目角色表单(mmm DRY!),但来自
标记内,并且具有空白项目角色。因为表单位于脚本标记内,所以它不会与项目表单一起提交,并且因为脚本类型是“text/x-jquery-tmpl”,所以这是完全有效的标记。
当用户单击“添加项目角色”时,它会触发一些采用模板内表单的 jQuery,将索引替换为当前日期(这就是可以唯一标识该项目角色的全部内容),并将其附加到表格的项目角色部分结束。
当用户单击项目角色旁边的“删除”时,它会检查该项目角色是否是新记录,如果不是,则会在表单末尾附加一个“_delete”隐藏字段。无论哪种情况,它都会从 DOM 中删除项目角色 div。
I don't have a web source that documents what I usually do, but I created a gist that documents what I do most often here: https://gist.github.com/900241
The premise of the gist is that you have a project model with many project roles, and you want to edit many project roles in the project form. This is pretty much the classic
accepts_nested_attributes_for
scenario, and just about any page that talks about it will give you a decent writeup. The problem is, the solutions I've seen have always involved some seriously messy obtrusive JavaScript that escaped your entire form view and threw it in the onClick method of a link. I recently came up with a cleaner unobtrusive approach using jQuery templates.You don't have to do a thing to your ProjectsController when you move to a nested model. Everything Just Works at the controller level, and you don't even need a ProjectRolesController. (This is why I didn't bother including them in the gist.) At the model level, it's just standard
accepts_nested_attributes_for
. Where it gets interesting is in the view.The project form has two form_for blocks: one rendering a jQuery template, and another rendering the project roles form. The jQuery template in turn just renders the project roles form (mmm DRY!), but from within a
<script>
tag, and with a blank project role. Because the form is within a script tag, it won't get submitted along with the project form, and because the script type is "text/x-jquery-tmpl", this is completely valid markup.When the user clicks on "Add a Project Role", it fires some jQuery that takes the form within the template, replaces the index with the current date (this is all so this project role can be uniquely identified), and appends it to the end of the project roles section of the form.
When the user clicks on "Delete" next to a project role, it checks to see if this project role is a new record, and if not, it appends a "_delete" hidden field to the end of the form. In either case, it removes the project role div from the DOM.