2012-07-27 65 views
0

我试图让一个简单的文件上传使用Carrierwave不能大规模指派保护属性:使用jQuery的文件上传,上传的轨道路径时

  • jQuery的文件上传护栏
  • carrierwave工作

我找不到以下错误

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文件上传位工作(我有引导按钮,图片出现在屏幕上等,只有文件上传本身显然不工作!)

为了完整,这是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 
+0

只想评论并说我发现了完全相同的功能。你有没有找到解决办法? – MoB 2012-08-01 23:40:05

+0

我自己想到了:请参阅[https://github.com/apotry/jquery_fileupload_carrierwave_fog](https://github.com/apotry/jquery_fileupload_carrierwave_fog)获取示例实现。 – apotry 2012-08-03 13:37:03

回答

2

该错误是由错误安装的上传器引起的。 有一个错字也许在:

mount_uploader :avater, AvatarUploader 

应该

mount_uploader :avatar, AvatarUploader 

试一下

+0

感谢您指出错字 - 修正了它,但错误信息仍然相同 – apotry 2012-07-27 15:13:28

相关问题