ruby-on-rails - rails : Photo uploads (Paperclip) with nesting & strong params

标签 ruby-on-rails file-upload paperclip nested-forms strong-parameters

背景:

我正在尝试使用带有 Paperclip gem 的 Strong Params 将照片上传添加到广告模型,其中 Photo 是一个单独的模型,它“has_attached_file”:upload,并且我在Ads#new view 将图像上传到 Photo 实例。参数看起来不错,但我想我在 Controller 中遗漏了一些东西。尽管 Controller 代码多次迭代,但仍无法使其正常工作。我需要做些什么不同的事情才能让它工作?

将不胜感激任何提示或指示!谢谢

——

广告模式:

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

——

照片型号:
 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

广告 Controller
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 %>

提交操作后的广告参数散列 (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

最佳答案

需要将表单指定为多部分才能使文件上传工作:

<%= semantic_form_for @ad, :html => {:multipart => true} do |form| %>

此外,您不允许传递临时文件:
params.require(:ad).
       permit(photos_attributes: 
               [upload: 
                 [:upload_file_name, :upload_content_type, :tempfile]
               ])

关于ruby-on-rails - rails : Photo uploads (Paperclip) with nesting & strong params,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463169/

相关文章:

ruby-on-rails - 运行测试时 has_many 通过 Factory Girl 错误

ruby-on-rails - Mongoid:缓存急切加载的文档

java - servlet文件上传文件名编码

ruby-on-rails - 如何使用回形针将图像保存为渐进图像?

excel - xls 和 xlsx 的回形针内容类型

ruby-on-rails - Rails 选择值

在 php.ini 上更改 upload_max_filesize 后,PHP 文件上传大小受到限制

javascript - 使用 jquery 进行多文件上传,有限制

ruby - 带回形针的圆角

php - 哪些支付处理框架(如 ActiveMerchant)可用于其他语言?