Reputation: 1538
I am relatively new to Rails and would appreciate any help.
My website accepts a signature image in base64 format and I am trying to use a Paperclip adaptor to decode the image and save it to my form
model as the :signature
attribute. I am using the advice given here (and here) which advises to use the following code:
In model:
class Thing
has_attached_file :image
In controller:
def create
image = Paperclip.io_adapters.for(params[:thumbnail_data])
image.original_filename = "something.gif"
Thing.create!(image: image)
...
end
My assumption is that Thing.create!
is setting the value of Paperclip's model attribute :image
to be the value of the image
variable whilst creating and saving a new Thing object. I tried to implement the same code in my FormsController
(create action) before @form.save
, but am receiving this error:
undefined method `before_image_post_process' for #<Class:0x007f94a2a26de8>
My 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
This is my Form
model:
class Form < ActiveRecord::Base
has_attached_file :signature
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
Upvotes: 2
Views: 3476
Reputation: 27961
Assuming that you're using the Rails form helpers over in your view, and based on your form_params
list, the :base64
key won't be at the top level of your params
hash, but rather one level down at params[:form][:base64]
Upvotes: 1