Rails 带照片的深层嵌套表单

Rails Deep Nested Form with Photos

我有一个深层嵌套表单,提供有一个包含照片的画廊。

offering.rb

has_one :gallery, foreign_key: "offering_id",:inverse_of => :offering, dependent: :destroy
accepts_nested_attributes_for :gallery, allow_destroy: true

gallery.rb

belongs_to :offering, foreign_key: "offering_id",:inverse_of => :gallery
has_many :photos, foreign_key: :gallery_id, dependent: :destroy, inverse_of: :gallery
accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image_file_name'].blank? }

photo.rb

belongs_to :gallery has_attached_file :image, :styles=>{:photo => "600x400#"}

offerings_controller.rb

def new
    @offering = Offering.new()
    gallery=@offering.build_gallery
    5.times{gallery.photos.build}
    @vendor=Vendor.find(params[:vendor_id])
end

def create
    @vendor=Vendor.find(params[:vendor_id])
    @offering=@vendor.offerings.create(offering_params)
    if @offering.save
        redirect_to vendor_path(params[:vendor_id])
    else
        gallery=@offering.build_gallery
        5.times{gallery.photos.build}
        render 'new'
    end
end

private
    def offering_params
        params.require(:offering).permit(gallery_attributes: [:id,:offering_id,{photos_attributes: [:id,:gallery_id,:image]}])
    end

offerings/new.html.erb

<%= form_for [@vendor,@offering],:html=>{:multipart => true} do |f| %>
    <%= f.fields_for :gallery do |g| %>
        Pictures for Gallery
        <%= g.fields_for :photos do |p| %>
            <%= p.file_field(:image) %>
        <% end %>
   <% end %>
<% end %>


Parameters: {"utf8"=>"✓", "authenticity_token"=>"AApeyzQbi6TxYYKFG/alcea+rK9s/MP2o8X4vJS741o=", "offering"=>{"gallery_attributes"=>{"photos_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fd2ea60aad0 @tempfile=#<Tempfile:/tmp/RackMultipart20150515-5596-nywang>, @original_filename="11261457_694846000643900_1047203131_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"offering[gallery_attributes][photos_attributes][0][image]\"; filename=\"11261457_694846000643900_1047203131_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}, "2"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fd2ea60a8c8 @tempfile=#<Tempfile:/tmp/RackMultipart20150515-5596-1d988rp>, @original_filename="11271093_695669323894901_362202707_n.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"offering[gallery_attributes][photos_attributes][2][image]\"; filename=\"11271093_695669323894901_362202707_n.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}}, "commit"=>"Add your listing", "vendor_id"=>"10"}}

问题是虽然图库保存在数据库中,但照片没有插入。它甚至没有尝试通过任何查询插入。

错误在

accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image_file_name'].blank? }

必须是

accepts_nested_attributes_for :photos, allow_destroy: true, reject_if: proc { |attributes| attributes['image'].blank? }

reject_if: proc { |attributes| attributes['image_file_name'].blank? } 需要在参数的实际散列中有一个属性,根据您的白名单,您只有 :id:gallery_id:image 可用。该拒绝必须是其中之一,否则将永远找不到。