Ruby on rails Rails:照片上传(回形针),带有嵌套和;强参数 背景:

Ruby on rails Rails:照片上传(回形针),带有嵌套和;强参数 背景:,ruby-on-rails,file-upload,paperclip,nested-forms,strong-parameters,Ruby On Rails,File Upload,Paperclip,Nested Forms,Strong Parameters,我正在尝试使用带回形针gem的强参数将照片上传添加到广告模型中,其中照片是一个单独的模型,它“有附加的文件”:upload,我在广告中调用forms for(实际上是语义forms for,因为我正在使用Formtastic)将图像上传到照片实例。Params看起来不错,但我想我错过了控制器中的一些东西。尽管控制器代码多次迭代,但仍无法使其正常工作。我需要做些什么才能让它正常工作 如果有任何提示或建议,我们将不胜感激!多谢各位 -- 广告模式: 路线 需要将表单指定为多部分,以便文件上载工作:

我正在尝试使用带回形针gem的强参数将照片上传添加到广告模型中,其中照片是一个单独的模型,它“有附加的文件”:upload,我在广告中调用forms for(实际上是语义forms for,因为我正在使用Formtastic)将图像上传到照片实例。Params看起来不错,但我想我错过了控制器中的一些东西。尽管控制器代码多次迭代,但仍无法使其正常工作。我需要做些什么才能让它正常工作

如果有任何提示或建议,我们将不胜感激!多谢各位

--

广告模式: 路线
需要将表单指定为多部分,以便文件上载工作:

<%= semantic_form_for @ad, :html => {:multipart => true} do |form| %>
params.require(:ad).permit(照片\属性::上传)


这应该足够了。

非常感谢你的回答,我试过了,但仍然得到了同样的结果。。。上传已经在没有multipart=true的情况下工作了(我认为在Rails 3.2+中是这样的,包括上传字段会自动设置这一点?)。允许:tempfile有道理,但似乎没有解决问题。我想知道这是否与我如何保存新的照片实例有关。。。
 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
 <%= 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 %>
{"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
<%= semantic_form_for @ad, :html => {:multipart => true} do |form| %>
params.require(:ad).
       permit(photos_attributes: 
               [upload: 
                 [:upload_file_name, :upload_content_type, :tempfile]
               ])