【问题标题】:500 error editing and updating multiple file uploads rails500错误编辑和更新多个文件上传rails
【发布时间】:2015-03-20 15:49:40
【问题描述】:

我正在使用 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']

end

listing.rb

has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true

attr_accessible :assets_attributes

listings/_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

【问题讨论】:

标签: ruby-on-rails paperclip multiple-file-upload


【解决方案1】:

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 ] }
}

【讨论】:

  • 修复了 500 错误,但后来我得到了这个:can't convert String into Integerapp/controllers/listings_controller.rb:86:in 'block in update'app/controllers/listings_controller.rb:85:in update'`
  • 我很高兴听到我的回答解决了您发布的问题(提示:)您是 StackOverflow 的新手,所以首先欢迎您,然后请阅读 What should I do when someone answers my question?。然后,在尝试了一些方法来解决您的新问题后,您可以发布一个包含详细信息的新问题。
【解决方案2】:

这解决了错误问题,但没有解决我的整体问题。我通过查看这篇文章找到了解决问题的方法:http://www.railscook.com/recipes/multiple-files-upload-with-nested-resource-using-paperclip-in-rails/

我需要改变处理多次上传的方式。

将我的输入更改为:

<%= file_field_tag "assets[]", type: :file, multiple: true, id: 'file-upload3' %>

并将以下内容添加到我的控制器中的创建和更新操作中:

if params[:assets]
    params[:assets].each { |asset|
      @listing.assets.create(asset: asset)
    }
end

做这些事情解决了我的问题。

【讨论】:

  • 这是一个糟糕的方法,将这个 hack 添加到您的控制器中,而不是使用您开始的 accepts_nested_attributes_for 正确地执行它。
猜你喜欢
  • 1970-01-01
  • 2011-01-31
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多