我有一个照片模型和一个画廊模型。画廊模型(我用漂亮的脚手架)只有一个领域,一个画廊名称。 在我的照片编辑表单中,我想将每张照片链接到图库,例如。我创建了2个独立的2009年和2010年的画廊,我希望每个照片编辑页面都有一个下拉列表以及画廊列表,因此每张照片都可以放在画廊中。 我一直在使用this 和this 作为起点,但我被卡住了,无法让画廊出现在我的照片中。fields_for nested form
class Gallery < ActiveRecord::Base
has_many :photos
attr_accessible :name
end
class Photo < ActiveRecord::Base
belongs_to :gallery
accepts_nested_attributes_for :gallery, :allow_destroy => true
的意见/照片/ _form.html.erb
<% form_for @photo, :html => { :multipart => true } do |photo_form| %>
<p>
<%= photo_form.label :title %><br />
<%= photo_form.text_field :title %>
</p>
<p>
<% photo_form.fields_for :gallery do |gal_form| %>
<%= gal_form.label :name %>
<%= gal_form.collection_select :gallery_id, Gallery.all, :id, :name %>
</p>
<% end %>
<p>
<%= submit_tag %>
</p>
<% end %>
目前有照片的形式页面上没有下拉列表中,但我没有收到任何错误,也没有它的提无论是在页面源。 我会很感激任何帮助或被指出正确的方向...
与我得到一个错误..未定义的方法'gallery_id' – 2010-07-08 17:25:01
你可以显示添加您的数据库架构?因为如果照片将在一个图库中,那么您的模型显示正确。但是你的照片列表中应该有一个gallery_id列。另外,只是为了确认,你有'@photo = Photo.find(照片的ID)'? – 2010-07-08 17:33:08
我没有galleries_id列,但我添加了一个,虽然它没有区别,并且照片的显示方法是正确的,@photo = Photo.find(params [:id]) – 2010-07-08 19:11:24