Rails:嵌套表单对象的单选按钮选择

发布于 2024-08-04 05:47:23 字数 1851 浏览 3 评论 0原文

我有以下 photo_album 表单,它使用 Rails 的嵌套表单功能在更新 photo_album 时保存照片。并且在选择单选按钮值时遇到问题。我希望只选择其中一张照片作为专辑封面,但由于 Rails 生成表单元素 id 和名称的方式,我可以选择所有照片作为专辑封面。有什么解决方法吗?

<% form_for @photo_album do |f| %>
  <%= f.error_messages %>

  <% @photo_album.photos.each do |photo| %>
    <% f.fields_for :photos, photo do |photo_fields| %>
      <p>
       <%= image_tag url_for_image_column(photo, "data", :thumb) %>
      </p>
      <p>
       <%= photo_fields.label :title %> 
       <%= photo_fields.text_field :title %>
      </p>
      <p>
       <%= photo_fields.label :body %>  
       <%= photo_fields.text_area :body %>
      </p>
      <p>
       <%= photo_fields.radio_button :cover, "1" %>
       <%= photo_fields.label :cover, 'Album Cover', :class => 'option' %>  
       <%= photo_fields.check_box :_delete %>
       <%= photo_fields.label :_delete, 'Delete', :class => 'option' %> 
      </p>  
     <% end %>
   <% end %>

   <p>
     <%= f.submit @photo_album.new_record? ? 'Create' : 'Update' %>
    </p>
<% end %>  

以下是 Rails 为单选按钮生成的 html(这是问题的一部分):

<p>
 <input type="radio" value="1" name="photo_album[photos_attributes][0][cover]" id="photo_album_photos_attributes_0_cover_1"/>
 <label for="photo_album_photos_attributes_0_cover" class="option">Album Cover</label>  
 <input type="hidden" value="0" name="photo_album[photos_attributes][0][_delete]"/><input type="checkbox" value="1" name="photo_album[photos_attributes][0][_delete]" id="photo_album_photos_attributes_0__delete"/>
 <label for="photo_album_photos_attributes_0__delete" class="option">Delete</label> 
</p>

I have the following form for photo_album which uses the nested forms feature of the Rails in saving the photos while updating the photo_album. And having trouble with the selection of radio button value. I want only one of the photos be able to select as the album cover, but due to the way rails produces form element ids and names, I am able to select all of the photos as album covers. Is there any workaround?

<% form_for @photo_album do |f| %>
  <%= f.error_messages %>

  <% @photo_album.photos.each do |photo| %>
    <% f.fields_for :photos, photo do |photo_fields| %>
      <p>
       <%= image_tag url_for_image_column(photo, "data", :thumb) %>
      </p>
      <p>
       <%= photo_fields.label :title %> 
       <%= photo_fields.text_field :title %>
      </p>
      <p>
       <%= photo_fields.label :body %>  
       <%= photo_fields.text_area :body %>
      </p>
      <p>
       <%= photo_fields.radio_button :cover, "1" %>
       <%= photo_fields.label :cover, 'Album Cover', :class => 'option' %>  
       <%= photo_fields.check_box :_delete %>
       <%= photo_fields.label :_delete, 'Delete', :class => 'option' %> 
      </p>  
     <% end %>
   <% end %>

   <p>
     <%= f.submit @photo_album.new_record? ? 'Create' : 'Update' %>
    </p>
<% end %>  

And following is the html produced by rails (which is part of the problem) for radio buttons:

<p>
 <input type="radio" value="1" name="photo_album[photos_attributes][0][cover]" id="photo_album_photos_attributes_0_cover_1"/>
 <label for="photo_album_photos_attributes_0_cover" class="option">Album Cover</label>  
 <input type="hidden" value="0" name="photo_album[photos_attributes][0][_delete]"/><input type="checkbox" value="1" name="photo_album[photos_attributes][0][_delete]" id="photo_album_photos_attributes_0__delete"/>
 <label for="photo_album_photos_attributes_0__delete" class="option">Delete</label> 
</p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2024-08-11 05:47:23

我猜想在这种情况下您会想要使用 radio_button_tag 改为并使用相同的名称。

这应该很接近 - 您仍然需要弄清楚何时要传递 true:

<%= radio_button_tag "cover", photo.id, todo %>

您也可以使用 radio_button 帮助器指定名称,但它不太适合模型,因此 radio_button_tag 对我来说更有意义 - 我可能是错的。

I would guess that in this instance you'd want to use radio_button_tag instead and use the same name.

This should be close - you will still need to figure out when you want to pass in true:

<%= radio_button_tag "cover", photo.id, todo %>

You might also be able to specify the name with the radio_button helper, but it doesn't quite fit the model so radio_button_tag makes more sense to me - I could be mistaken.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文