2013-10-15 35 views
3

請幫我...我用回形針上傳畫布標記(base64)中的1張圖片到aws-s3。回形針與base64:undefined方法`stringify_keys'爲#<字符串:0xb46dba14>

我的控制器

def create 
    decoded_file = Base64.decode64(params[:photo]) 
     begin 
     file = Tempfile.new(['test', '.jpg']) 
     file.binmode 
     file.write decoded_file 
     file.close 
     @photo.photo = file 
     if @photo.save 
      render :json => {:message => "Successfully uploaded the profile picture."} 
     else 
      render :json => {:message => "Failed to upload image"} 
     end 
     ensure 
     file.unlink 
     end 
    end 

型號

class Photo < ActiveRecord::Base 
    has_attached_file :photo, styles: { thumbnail: "150x200#"}, default_style: :thumbnail 
end 

和錯誤:

NoMethodError at /photos 
=================================== 
> undefined method `stringify_keys' for #<String:0xb46dba14> 
activerecord (4.0.0) lib/active_record/attribute_assignment.rb, line 17 

回答

2

好吧,我想我有東西。

有2個潛在的問題:

  1. 你的文件(用帆布)的創建可能是不正確的
  2. @post.save功能可能不正確

我不知道帆布的東西....所以我會給你我最好的拍攝與@post.save

def create 
    decoded_file = Base64.decode64(params[:photo]) 
     begin 
     file = Tempfile.new(['test', '.jpg']) 
     file.binmode 
     file.write decoded_file 
     file.close 

     params[:photo] = file 

     @photo.new(photo_params) 
     if @photo.save 
      render :json => {:message => "Successfully uploaded the profile picture."} 
     else 
      render :json => {:message => "Failed to upload image"} 
     end 
     ensure 
     file.unlink 
     end 
    end 

    private 
    def photo_params 
     params.permit(:photo) 
    end 
+0

現在,工作很好。但我刪除行「file.close」。非常感謝你。 – JohnEvans

+0

我很抱歉,因爲我沒有投票給你。因爲投票需要15個代表。 :D – JohnEvans

+0

hehe我剛剛提出了你的問題,現在你只需要再次投票! :) –

1

是U使用updat以這種方式e_attributes()或build()?

update_attribures(PARAMS [:照片])

你如果是的話,你應該使用這樣的:

update_attributes方法(:照片=> PARAMS [:照片])

希望能爲您提供幫助和幫助。

+0

感謝您的幫助。但我沒有以這種方式使用update_attributes()或build()。任何其他方式? – JohnEvans

相關問題