简体   繁体   English

在Rails中创建嵌套视图的正确方法是什么

[英]What is the right way of creating a nested view in rails

I have blog model. 我有博客模型。 Blog has_many comments. 博客has_many评论。 I have created all the CRUD related to the blog. 我已经创建了与博客相关的所有CRUD。 The comments doesnt have a page on its own. 评论没有单独的页面。 On the blog page, there could be text area and on the entering the comment, it would be saved thru ajax. 在博客页面上,可能会有文本区域,在输入评论时,它将通过ajax保存。 But normally when a new page is created a new object is sent from the controller, so should i create a comment object and send it thru Blog's new action like this 但是通常在创建新页面时,会从控制器发送一个新对象,所以我应该创建一个注释对象并通过Blog的新操作将其发送

  def new
    @comment = Comment.new
    @blog = Blog.new
  end

Or should i just access the comment objects present in the blog while creating the view 或者我应该在创建视图时仅访问博客中存在的评论对象

<form_remote_for @blog.comments>

Which is the right way of doing this? 哪个是正确的方法? Is there any better solution 有没有更好的解决方案

Its preferred to have initialization of new comment in controller action. 首选在控制器操作中初始化新注释。 But its rather a guideline or practice I follow rather than the rule. 但是我遵循的是指导原则或实践,而不是规则。

There is no form_remote_for tag. 没有form_remote_for标签。 If its rails 2, tag is remote_form_for , similar thing in rails 3 would be: 如果它的rails 2的标记是remote_form_for ,则rails 3中的类似内容将是:

form_for [@blog, @comment], :remote => true do |f|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM