简体   繁体   English

Rails-以“更新”形式显示嵌套属性

[英]Rails - Display Nested Attributes in an “Update” Form

I am having a bit of difficulty getting my "update form" to display the nested attributes. 我在获取“更新表单”以显示嵌套属性时遇到了一些困难。 Specifically, images (eg, "choices") to display. 具体地,显示图像(例如,“选择”)。 All other data fields are showing. 显示所有其他数据字段。 Just not this is my form: 只是这不是我的表格:

<%= bootstrap_form_for @template, :url => {:action => 'update', :id => @template.id } do |f| %>
 <fieldset>
  <legend>Update Your Template</legend>
  <%= f.text_field :prompt, :class => :span6, :placeholder => "Which one is running?", :autocomplete => :off %>
  <%= f.select 'group_id', options_from_collection_for_select(@groups, 'id', 'name', selected: @template.group.id) %>
   <div class="row-fluid">
    <ul class="thumbnails">
      <%= f.fields_for :template_assignments do |builder| %>
       <li class="span3" id="">
         <div class="thumbnail">
           <%= builder.text_field :choice_id %>
           <%= image_tag @template.template_assignments.builder.choice.image %>
         </div>
        </li>
      <% end %>
     </ul>
   </div>
 <% end %>

The main line I am having trouble with is: 我遇到的主要问题是:

<%= image_tag @template.template_assignments.builder.choice.image %>

I cannot get it to iterate through each of the 4 nested attributes for the image. 我无法遍历图像的4个嵌套属性中的每个属性。 It iterates through the 4 nested attributes pertaining to :choice_id, which displays correctly in the text_field. 它遍历与:choice_id有关的4个嵌套属性,这些属性正确显示在text_field中。

If i change it to: 如果我将其更改为:

<%= image_tag @template.template_assignments.first.choice.image %> , it displays the first image no problem. <%= image_tag @template.template_assignments.first.choice.image %> ,它显示第一个图像没有问题。

However, I need it to iterate and display the "first", "second", "third", and "fourth" images. 但是,我需要它来迭代并显示“第一”,“第二”,“第三”和“第四”图像。

Any help on how to display these images, just as the image_id's are being displayed? 就如何显示这些图像有什么帮助,就像正在显示image_id一样?

EDIT: Here are my models 编辑:这是我的模型

# app/models/template.rb

class Template < ActiveRecord::Base
 belongs_to :group
 has_many :template_assignments, dependent: :destroy
 has_many :choices, :through => :template_assignments
 accepts_nested_attributes_for :template_assignments, allow_destroy: true
end

# app/models/template_assignment.rb

class TemplateAssignment < ActiveRecord::Base
 belongs_to :template
 belongs_to :choice
end

# app/models/choice.rb

 class Choice < ActiveRecord::Base
  has_many :template_assignments
  has_many :templates, :through => :template_assignments
end

You'll probably want to just use builder directly, just like you're doing in the text_field . 您可能只想直接使用builder ,就像在text_field

<%= image_tag builder.choice.image %>

[UPDATE] after some trial and error the correct form would be : [更新]经过反复试验,正确的格式为:

<%= image_tag builder.object.choice.image %>

What's happening is that when f.fields_for :template_assignments do |builder| 发生的是,当f.fields_for :template_assignments do |builder| is used to render the nested items, the builder object that is yield ed to the block is not the object itself (in this case a TemplateAssignment ), but is a FormBuilder object, which is what supplies the convenience methods like builder.text_field . 用于渲染嵌套项目, yield块的builder对象不是对象本身(在本例中为TemplateAssignment ),而是FormBuilder对象,它提供了诸如builder.text_field类的便捷方法。 (If you tried to do template_assignment.text_field you'd get an error.) The builder stores the object that it is representing in the form as object , so you can get a hold of your template_assignment object by using builder.object . (如果尝试执行template_assignment.text_field ,则会出错。) builder将其在表单中表示的对象存储为object ,因此可以使用builder.object保留template_assignment对象。 From there you can deal the the template_assignment like normal. 从那里,您可以像往常一样处理template_assignment I hope that helps. 希望对您有所帮助。

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

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