Ruby on rails 为什么我不能在这里构建多个嵌套属性?
这是我的表格代码:Ruby on rails 为什么我不能在这里构建多个嵌套属性?,ruby-on-rails,ruby,ruby-on-rails-3,nested-attributes,Ruby On Rails,Ruby,Ruby On Rails 3,Nested Attributes,这是我的表格代码: <%= simple_form_for setup_video(@video) do |f| %> <% f.fields_for :comment_titles do |t| %> <%= t.input :title, :label => "Comment Title:" %> <%= t.button :submit, :value => 'Add', :id => 'add_comment_t
<%= simple_form_for setup_video(@video) do |f| %>
<% f.fields_for :comment_titles do |t| %>
<%= t.input :title, :label => "Comment Title:" %>
<%= t.button :submit, :value => 'Add', :id => 'add_comment_title' %>
<div class='hint'>Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!</div>
<% end %>
<% end %>
我认为这才是真正需要的:
def update
@video = current_user.videos.find(params[:id])
respond_to do |format|
if @video.update_attributes(params[:video])
format.html { redirect_to(@video) }
format.js
else
format.html { render :action => "edit" }
end
end
end
此处的编辑操作将提供一个表单,允许您编辑现有记录及其嵌套属性。这就是它替换现有对象的原因
如果您只希望人们添加新的评论标题,那么我建议您在编辑操作中创建一个新对象,如下所示:
def edit
video = current_user.videos.find(params[:id])
video.comment_titles.build
end
然后,这将作为字段_中的附加行提供,以供调用。要仅显示新对象,请执行以下操作:
<% f.fields_for :comment_titles do |t| %>
<% if t.object.new_record? %>
# stuff goes here
<% end %>
<% end %>
但是,这限制了人们只能在编辑操作中添加新项目,这可能会违反某些用户的直觉。您必须向我们提供更多代码,例如此字段周围的表单以及它发布到的操作。要添加到Ryan的评论,还有显示表单的操作。@Andrew显示表单的操作?您发布了它,它是“新建”或“编辑”操作。嵌套属性的设计似乎是为了使每个新属性都需要一个新表单?那不是我真正想要的。。。
<% f.fields_for :comment_titles do |t| %>
<% if t.object.new_record? %>
# stuff goes here
<% end %>
<% end %>