我正在使用Paperclip,我已经为我的一个模型添加了多个文件上传。 一切正常,除非我尝试将新文件添加到已上传的现有文件中。 它抛出此错误:
Unexpected error while processing request: expected Hash (got Array) for param `assets_attributes'我该如何解决这个问题,以便我可以添加新文件? 提前致谢。
asset.rb
class Asset < ActiveRecord::Base belongs_to :member belongs_to :listing attr_accessible :asset has_attached_file :asset, styles: { large: "700x700>", thumb: "100x100#" } validates_attachment_size :asset, :less_than_or_equal_to=>10.megabyte validates_attachment_content_type :asset, :content_type=>['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] endlisting.rb
has_many :assets, :dependent => :destroy accepts_nested_attributes_for :assets, :allow_destroy => true attr_accessible :assets_attributes列表/ _edit_form.html.erb
<%= simple_form_for(@listing, :html => { class: 'form-horizontal ', :multipart => true }) do |f| %> <% if @listing.errors.any? %> <%= f.error_notification %> <div> <%= file_field_tag('listing_assets_asset', multiple: true, name: "listing[assets_attributes][][asset]", id: 'file-upload3', class: '') %> </div> <% end %>listings_controller.rb
before_filter :authenticate_member!, only: [:new, :create, :edit, :update, :destroy] before_filter :find_member before_filter :find_listing, only: [:edit, :update, :destroy] def new @listing = Listing.new respond_to do |format| format.html # new.html.erb format.json { render json: @listing } end end # GET /listings/1/edit def edit end # POST /listings # POST /listings.json def create @listing = current_member.listings.new(params[:listing]) respond_to do |format| if @listing.save current_member.create_activity(@listing, 'created') format.html { redirect_to @listing } format.json { render json: @listing, status: :created, location: @listing } else format.html { render action: "new" } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end # PUT /listings/1 # PUT /listings/1.json def update respond_to do |format| if @listing.update_attributes(params[:listing]) format.html { redirect_to @listing } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end private def find_member @member = Member.find_by_user_name(params[:user_name]) end def find_listing @listing = current_member.listings.find(params[:id]) endI'm using Paperclip and I've added multiple file uploads to one of my models. Everything works fine except when I try to add new files to the existing ones already uploaded. It throws this error:
Unexpected error while processing request: expected Hash (got Array) for param `assets_attributes'How can I fix this so that I can add new files? Thanks in advance.
asset.rb
class Asset < ActiveRecord::Base belongs_to :member belongs_to :listing attr_accessible :asset has_attached_file :asset, styles: { large: "700x700>", thumb: "100x100#" } validates_attachment_size :asset, :less_than_or_equal_to=>10.megabyte validates_attachment_content_type :asset, :content_type=>['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] endlisting.rb
has_many :assets, :dependent => :destroy accepts_nested_attributes_for :assets, :allow_destroy => true attr_accessible :assets_attributeslistings/_edit_form.html.erb
<%= simple_form_for(@listing, :html => { class: 'form-horizontal ', :multipart => true }) do |f| %> <% if @listing.errors.any? %> <%= f.error_notification %> <div> <%= file_field_tag('listing_assets_asset', multiple: true, name: "listing[assets_attributes][][asset]", id: 'file-upload3', class: '') %> </div> <% end %>listings_controller.rb
before_filter :authenticate_member!, only: [:new, :create, :edit, :update, :destroy] before_filter :find_member before_filter :find_listing, only: [:edit, :update, :destroy] def new @listing = Listing.new respond_to do |format| format.html # new.html.erb format.json { render json: @listing } end end # GET /listings/1/edit def edit end # POST /listings # POST /listings.json def create @listing = current_member.listings.new(params[:listing]) respond_to do |format| if @listing.save current_member.create_activity(@listing, 'created') format.html { redirect_to @listing } format.json { render json: @listing, status: :created, location: @listing } else format.html { render action: "new" } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end # PUT /listings/1 # PUT /listings/1.json def update respond_to do |format| if @listing.update_attributes(params[:listing]) format.html { redirect_to @listing } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end private def find_member @member = Member.find_by_user_name(params[:user_name]) end def find_listing @listing = current_member.listings.find(params[:id]) end最满意答案
accepts_nested_attributes_for期望它处理的*_attributes值的哈希值,它所期望的哈希值将数组索引作为键,即。 它期望这样的事情:
asset_attributes: { 0 => { asset: value_for_0 }, 1 => { asset: value_for_1 } }通过创建表单字段的名称listing[assets_attributes][][asset]您实际上是在创建一个数组,即。
asset_attributes: [ { asset: value_for_0 }, { asset: value_for_1 } ]这就是您收到错误的原因。
我认为你的名字是什么意思: listing[assets_attributes][asset][]这将创建:
asset_attributes: { 0 => { asset: [ array, of, IO, objects, for, your, files ] } }That fixed the error issue but did not fix my overall problem. I found the fix to my problem by looking at this article: http://www.railscook.com/recipes/multiple-files-upload-with-nested-resource-using-paperclip-in-rails/
I needed to change the way I was handling multiple uploads.
Changing my input to this:
<%= file_field_tag "assets[]", type: :file, multiple: true, id: 'file-upload3' %>and adding the following to my create and update actions in my controler:
if params[:assets] params[:assets].each { |asset| @listing.assets.create(asset: asset) } endDoing these things resolved my issue.
更多推荐
发布评论