0
我有以下用於photo_album的表單,它在更新photo_album時使用Rails的嵌套表單功能來保存照片。並且無法選擇單選按鈕值。我只希望其中一張照片可以選擇作爲專輯封面,但由於rails生成表單元素ID和名稱的方式,我可以選擇所有照片作爲專輯封面。有什麼解決方法嗎?Rails:用於嵌套窗體對象的單選按鈕
<% 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 %>
而下面是軌道生成的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>