Rails:使用嵌套和强参数上传照片(Paperclip)

8

背景:

我正在尝试使用Strong Params和Paperclip gem将照片上传到广告模型,其中Photo是一个单独的模型,它"has_attached_file":upload,并且我在Ads#new视图中调用forms_for(实际上是semantic_forms_for,因为我正在使用Formtastic)来将图片上传到Photo实例。参数看起来不错,但我认为我在控制器中漏掉了一些东西。尽管多次迭代控制器代码,但我还无法使其正常工作。我需要做些什么不同的事情才能让它正常工作呢?

非常感谢任何提示或指针!谢谢。

--

广告模型:

class Ad < ActiveRecord::Base
  belongs_to :user

    ##edited out irrelevant associations

  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
  default_scope { order("created_at DESC") }
  #validates_presence_of :user_id, :make, :model, :km, :year, :location
end

--

Photo model:

 class Photo < ActiveRecord::Base
      belongs_to :ad
      has_attached_file :upload, :styles => { :main => "600x500>", 
                                                                                    :thumb => "60x45>" }, 
      :default_url => "http://placehold.it/600x500&text=nice+wheels!"
    end

广告控制器

class AdsController < ApplicationController

    def create
        @ad = Ad.new(ad_params)
      @ad.user_id = current_user.id
      @photo = @ad.photos.build
        if @ad.save
            redirect_to @ad
        else
            flash.alert="You were missing some super-important details. Try again"
            redirect_to new_ad_path
        end
    end

    private

    def ad_params ##excluded all attributes except nested upload attrib. for gist
        params.require(:ad).permit(photos_attributes: [upload: [:upload_file_name, :upload_content_type]])
    end

end

Ads#new

 <%= semantic_form_for @ad do |form| %>

    <!-- left out all other form fields -->      

      <%= form.semantic_fields_for :photo do |photo| %>
        <%= photo.file_field :upload %>
      <% end %>

    <% end %>

广告提交操作后的广告参数哈希值(Ad#create)

{"location":"Abha","make":"Bentley","model":"","gear":"","km":"50","price_bid":"500","price_ask":"500","ac":"0","cd":"0","radio":"0","powersteering":"0","abs":"0","satnav":"0","cruise":"0","fwd":"0","convertible":"0","problem_1":"","problem_2":"","problem_3":"","description":"","photo":{"upload":{"original_filename":"mustang-290x218.jpg","content_type":"image/jpeg","headers":"Content-Disposition: form-data; name=\"ad[photo][upload]\"; filename=\"mustang-290x218.jpg\"\r\nContent-Type: image/jpeg\r\n","tempfile":[]}}}

路由

resources :ads, only: [:new, :create, :show, :index, :edit, :destroy] do
        resources :comments, only: [:create, :destroy]
        resources :photos, only: [:create, :destroy]
        resources :favorite_ads, only: [:create, :destroy]
        resource :emails, only: :create 
  end
2个回答

2
表单需要指定为多部分才能上传文件:
<%= semantic_form_for @ad, :html => {:multipart => true} do |form| %>

此外,您没有允许传递tempfile:
params.require(:ad).
       permit(photos_attributes: 
               [upload: 
                 [:upload_file_name, :upload_content_type, :tempfile]
               ])

非常感谢您的回答,我尝试了但仍然得到相同的结果...上传已经在没有multipart = true的情况下工作(我认为这在Rails 3.2+中是正确的,包括上传字段会自动设置这个?)。允许:tempfile是有道理的,但似乎并没有解决问题。我想知道是否与我保存新照片实例的方式有关... - jawad-uk

1

params.require(:ad).permit(photos_attributes: :upload)

这应该足够了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接