使用 Paperclip 上传 base64 图像 - Rails 4
Upload base64 Image with Paperclip - Rails 4
我对 Rails 比较陌生,希望得到任何帮助。
我的网站接受 base64 格式的签名图像,我正在尝试使用 Paperclip 适配器解码图像并将其作为 :signature
属性保存到我的 form
模型中。我正在使用给出的建议 here (and here),建议使用以下代码:
模型中:
class Thing
has_attached_file :image
在控制器中:
def create
image = Paperclip.io_adapters.for(params[:thumbnail_data])
image.original_filename = "something.gif"
Thing.create!(image: image)
...
end
我的假设是 Thing.create!
将 Paperclip 的模型属性 :image
的值设置为 image
变量的值,同时创建和保存新的 Thing 对象。我试图在 @form.save
之前在我的 FormsController
(创建操作)中实现相同的代码,但收到此错误:
undefined method `before_image_post_process' for #<Class:0x007f94a2a26de8>
我的FormsController
:
class FormsController < ApplicationController
before_action :logged_in_user
before_action :admin_user, only: :destroy
def index
@forms = Form.all #paginate
end
def show
@form = Form.find(params[:id])
end
def new
@form = Form.new
end
def create
@form = Form.new(form_params)
# Paperclip adaptor
signature = Paperclip.io_adapters.for(params[:base64])
signature.original_filename = "something.png"
# Attempt to submit image through Paperclip
@form.signature = signature
if @form.save
flash[:success] = "The form has been successfully created!"
redirect_to @form
else
render 'new'
end
end
def edit
@form = Form.find(params[:id])
end
def update
@form = Form.find(params[:id])
if @form.update_attributes(form_params)
flash[:success] = "Form has been updated!"
redirect_to @form
else
render 'edit'
end
end
def destroy
Form.find(params[:id]).destroy
flash[:success] = "Form deleted"
redirect_to forms_path
end
private
def form_params
params.require(:form).permit(:first_name, :last_name, :email, :phone, :address, :member_type, :base64)
end
end
这是我的 Form
型号:
class Form < ActiveRecord::Base
has_attached_file :signature
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
假设您在视图中使用 Rails 表单助手,并且根据您的 form_params
列表,:base64
键不会位于顶层您的 params
哈希值,而是在 params[:form][:base64]
下一级
我对 Rails 比较陌生,希望得到任何帮助。
我的网站接受 base64 格式的签名图像,我正在尝试使用 Paperclip 适配器解码图像并将其作为 :signature
属性保存到我的 form
模型中。我正在使用给出的建议 here (and here),建议使用以下代码:
模型中:
class Thing
has_attached_file :image
在控制器中:
def create
image = Paperclip.io_adapters.for(params[:thumbnail_data])
image.original_filename = "something.gif"
Thing.create!(image: image)
...
end
我的假设是 Thing.create!
将 Paperclip 的模型属性 :image
的值设置为 image
变量的值,同时创建和保存新的 Thing 对象。我试图在 @form.save
之前在我的 FormsController
(创建操作)中实现相同的代码,但收到此错误:
undefined method `before_image_post_process' for #<Class:0x007f94a2a26de8>
我的FormsController
:
class FormsController < ApplicationController
before_action :logged_in_user
before_action :admin_user, only: :destroy
def index
@forms = Form.all #paginate
end
def show
@form = Form.find(params[:id])
end
def new
@form = Form.new
end
def create
@form = Form.new(form_params)
# Paperclip adaptor
signature = Paperclip.io_adapters.for(params[:base64])
signature.original_filename = "something.png"
# Attempt to submit image through Paperclip
@form.signature = signature
if @form.save
flash[:success] = "The form has been successfully created!"
redirect_to @form
else
render 'new'
end
end
def edit
@form = Form.find(params[:id])
end
def update
@form = Form.find(params[:id])
if @form.update_attributes(form_params)
flash[:success] = "Form has been updated!"
redirect_to @form
else
render 'edit'
end
end
def destroy
Form.find(params[:id]).destroy
flash[:success] = "Form deleted"
redirect_to forms_path
end
private
def form_params
params.require(:form).permit(:first_name, :last_name, :email, :phone, :address, :member_type, :base64)
end
end
这是我的 Form
型号:
class Form < ActiveRecord::Base
has_attached_file :signature
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
假设您在视图中使用 Rails 表单助手,并且根据您的 form_params
列表,:base64
键不会位于顶层您的 params
哈希值,而是在 params[:form][:base64]