【问题标题】:Carrierwave Can't mass-assign protected attributes: path when uploading using jquery-fileupload-railsCarrierwave 无法批量分配受保护的属性:使用 jquery-fileupload-rails 上传时的路径
【发布时间】:2012-07-26 06:37:12
【问题描述】:

我正在尝试使用

进行简单的文件上传
  • jquery-fileupload-rails
  • 载波

我找不到以下错误的原因

Started POST "/pictures" for 127.0.0.1 at 2012-07-27 15:19:37 +0100
Processing by PicturesController#create as JSON
  Parameters: {"utf8"=>"✓",
  authenticity_token"=>"NKek0e/kfVRUk4SVBjokO5rL446dKvHo9+7mKuH0HKs=", "picture"=>   
  {"path"=>#  <ActionDispatch::Http::UploadedFile:0x00000002d48fa8 
  @original_filename="IMG_0001.JPG", @content_type="image/jpeg", @headers="Content-
  Disposition: form-data; name=\"picture[path]\"; filename=\"IMG_0001.JPG\"\r\nContent-
  Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120727-7807-1bcbfof>>}}
Completed 500 Internal Server Error in 1ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: 
path):
  app/controllers/pictures_controller.rb:9:in `new'
  app/controllers/pictures_controller.rb:9:in `create'

模型、控制器和视图取自

https://github.com/blueimp/jQuery-File-Upload/wiki/Rails-setup-for-V6

错误消息让我认为我需要使“路径”可访问,但如何?请注意,jquery-file-upload 位正在工作(我有引导按钮,图片出现在屏幕上等,只有文件上传本身显然不起作用!)

为了完整起见,这是picture.rb:

class Picture < ActiveRecord::Base

  include Rails.application.routes.url_helpers
  attr_accessible :avatar
  mount_uploader :avatar, AvatarUploader

  def to_jq_upload
    {
      "name" => read_attribute(:avatar),
      "size" => avatar.size,
      "url" => avatar.url,
      "thumbnail_url" => avatar.thumb.url,
      "delete_url" => picture_path(:id => id),
      "delete_type" => "DELETE" 
    }
  end
end

对应的控制器是

class PicturesController < ApplicationController  

  def index
    @pictures = Picture.all
    render :json => @pictures.collect { |p| p.to_jq_upload }.to_json
  end

  def create
    @picture = Picture.new(params[:picture])
    @picture.save!
    if @picture.save
      respond_to do |format|
        format.html {  
          render :json => [@picture.to_jq_upload].to_json, 
          :content_type => 'text/html',
          :layout => false
        }
        format.json {  
          render :json => [@picture.to_jq_upload].to_json     
        }
      end
    else 
      render :json => [{:error => "custom_failure"}], :status => 304
    end
  end

  def destroy
    @picture = Picture.find(params[:id])
    @picture.destroy
    render :json => true
  end

end

这是上传器类:

class AvatarUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end


end

【问题讨论】:

标签: ruby-on-rails file-upload carrierwave fog


【解决方案1】:

该错误是由安装错误的上传器引起的。 可能有错别字:

mount_uploader :avater, AvatarUploader

应该是

mount_uploader :avatar, AvatarUploader

试试看

【讨论】:

  • 感谢指出错字 - 已修复,但错误信息仍然相同
【解决方案2】:

我自己想出了解决方案:错误出现在视图中,而不是 :path 它应该显示为 :avatar,并且 :avatar 需要可访问。可以在https://github.com/apotry/jquery_fileupload_carrierwave_fog找到一个还设置了 jQuery File Upload 选项的示例实现

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多