请帮帮我...我用回形针将canvas标签(base64)中的1张图片上传到aws-s3。
我的控制器
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
答案 0 :(得分: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
答案 1 :(得分:1)
您是否以这种方式使用update_attributes()或build()?
update_attribures(PARAMS [:相片])
如果你这样,你应该使用它:
update_attributes(:photo =&gt; params [:photo])
希望这对你有所帮助。