Rails:嵌套表单对象的单选按钮选择
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想在这种情况下您会想要使用
radio_button_tag
改为并使用相同的名称。这应该很接近 - 您仍然需要弄清楚何时要传递 true:
您也可以使用 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:
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.