我有一个模特,一个有趣的图片。我希望能够为我的数据库中的每个pointofinterest选择一个缩略图,以便在我的网站中使用。
问题是我的单选按钮不像单选按钮组。它允许我在生成表单时选择多个单选按钮。
我尝试过使用radio_button而不是picture.radio_button,并将它们关联为一个组。我在这里错过了什么吗?
<%= f.fields_for :pictures do |picture| %>
<% unless picture.object.new_record? %>
<%= link_to( image_tag(picture.object.photo.url(:thumb)), picture.object.photo.url(:original) ) %>
<div class="checkbox inline">
<%= picture.label :destroy%>
<%= picture.check_box :_destroy %>
<%if :pictures[0] == picture %>
<%= picture.label :thumbnail, :value => true%>
<%= picture.radio_button :thumbnail, 'thumbnail', :checked => true %>
<% else %>
<%= picture.label :thumbnail%>
<%= picture.radio_button :thumbnail, 'thumbnail', :checked => false %>
<%end%>
</div>
<% end %>
<% end %>
编辑 - 完整_形式部分:
<%= form_for(@pointofinterest, :html => { :multipart => true }) do |f| %>
<% if @pointofinterest.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pointofinterest.errors.count, "error") %> prohibited this pointofinterest from being saved:</h2>
<ul>
<% @pointofinterest.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :latitude %>
<%= f.text_field :latitude %>
</div>
<div class="field">
<%= f.label :longitude %>
<%= f.text_field :longitude %>
</div>
<div class="">
<%= f.fields_for :pictures do |picture| %>
<% if picture.object.new_record? %>
<%= picture.file_field :photo %>
<%end%>
<% end %>
</div>
<div class="existingPaperclipFiles">
<% @pointofinterest.pictures.each do |picture| %>
<% unless picture.nil? %>
<%= link_to( image_tag(picture.photo.url(:thumb)), picture.photo.url(:original) ) %>
<div class="checkbox inline">
<%= f.check_box :_destroy %><%= f.label :Delete%>
<% checked = (@pointofinterest.thumbnail_id == picture.id) ? "true" : "false" %>
<%= f.radio_button :thumbnail_id, 'thumb', :checked => checked %> <%= f.label :Thumb %>
</div>
<%end%>
<% end %>
</div>
<div class="actions">
<%= f.submit "Submit"%>
</div>
<% end %>
答案 0 :(得分:0)
我认为你不需要fields_for
用于此
fields_for
适用于其他模型 - 我认为您希望在“父”模型中选择:thumbnail_id
属性:
#app/models/point_of_interest.rb
Class PointOfInterest < ActiveRecord::Base
has_many :pictures
belongs_to :thumbnail, class_name: "Picture"
end
#app/models/picture.rb
Class Picture < ActiveRecord::Base
belongs_to :point_of_interest
has_many :points_of_interest, class_name: "PointOfInterest", foreign_key: "thumbnail_id"
end
#app/views/controller/action.html.erb
<%= @pictures.each do |picture| %>
<%= link_to( image_tag(picture.object.photo.url(:thumb)), picture.object.photo.url(:original) ) %>
<div class="checkbox inline">
<%= f.label :destroy%>
<%= f.check_box :_destroy %>
<% checked = (@original.thumbnail_id == picture.id) ? "true" : "false" %>
<%= f.label :thumbnail_id %>
<%= f.radio_button :thumbnail_id, 'thumbnail', checked : checked %>
</div>
<% end %>