ruby on rails - Paperclip image upload (base64) - getting 500 internal server error (undefined method 'permit') -


i'm trying upload image using paperclip; followed paperclip quick start guide closely possible, i'm doing wrong , can't find out what. i'm new ruby , rails; whole project set angularjs framework.

what i'm doing sending post request contains base64 encoded image:

dataservice.add("uploads", {image: file}) [...] 

the service looks this:

add: function(type, object) {         return $http.post(api_url + "/" + type, object) [...] 

"file" base64 encoded image; hope clear how post works. backend:

controller:

module api class uploadscontroller < applicationcontroller      # post api/uploads     def create         p params         @upload = upload.create(upload_params)         @upload.save     end      private          def upload_params             params.require(:upload).permit(:image)         end end end 

model:

class upload < activerecord::base attr_accessor :content_type, :original_filename, :image_data before_save :decode_base64_image  has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/storage/images/:style/missing.png", :url => "/:class/:attachment/:id/:style_:basename.:extension"  validates_attachment_content_type :image, :content_type => /\aimage\/.*\z/ validates :image, :attachment_presence => true # validates_attachment :image, :size => { :in => 0..100.kilobytes }  protected      def decode_base64_image         if image_data && content_type && original_filename             decoded_data  = base64.decode64(image_data)              data = stringio.new(decoded_data)             data.class_eval                 attr_accessor :content_type, :original_filename             end              data.content_type = content_type             data.original_filename = file.basename(original_filename)              self.image = data         end     end end 

that's - hope included that's important problem (first question here).

when testing simple image 500 - internal server error, request payload

{"image":"data:image/png;base64,...<base64 code>...} 

i understand mistake in controller/model part, don't know how solve this.

thanks :)


edit:

when testing locally postman (form-data, post, key: upload , image) this:

started post "/api/uploads" 127.0.0.1 @ 2015-06-13 16:34:08 +0200 processing api::uploadscontroller#create */* parameters: {"upload"=>#<actiondispatch::http::uploadedfile:0x00000002f0e108 @tempfile=#     <tempfile:/tmp/rackmultipart20150613-7296-8zu01e.jpeg>,     @original_filename="meise.jpeg", @content_type="image/jpeg",    @headers="content-disposition: form-data; name=\"upload\"; filename=\"meise.jpeg\"\r\ncontent-type: image/jpeg\r\n">} {"upload"=>#<actiondispatch::http::uploadedfile:0x00000002f0e108 @tempfile=#<tempfile:/tmp/rackmultipart20150613-7296-8zu01e.jpeg>, @original_filename="meise.jpeg", @content_type="image/jpeg", @headers="content-disposition: form-data; name=\"upload\"; filename=\"meise.jpeg\"\r\ncontent-type: image/jpeg\r\n">, "controller"=>"api/uploads", "action"=>"create"} completed 500 internal server error in 0ms  nomethoderror (undefined method `permit' #    <actiondispatch::http::uploadedfile:0x00000002f0e108>): app/controllers/api/uploads_controller.rb:16:in `upload_params' app/controllers/api/uploads_controller.rb:7:in `create' 

edit 2:

it works now, same problem: if you're sending base64 encoded image i'm doing in dataservice , having same controller/module setup, work paperclip, not frontend. in

has_attached_file :image, :styles => { :medium => "1000x1000>", :thumb => "150x150#" }, :default_url => "/storage/images/:style/missing.png", :url => "/:class/:attachment/:id/:style_:basename.:extension" 

i had change last part to

"/:class/:attachment/:id/:style_image_:id.png" 

because somehow name , extension got lost somewhere. used id file name now, , .png extension because base64 created png created in frontend.

the problem line:

params.require(:upload).permit(:image) 

require ensures parameter present. if it's present, returns parameter @ given key. in case returning actiondispatch::http::uploadedfile object doesn't have permit method.

you can solve removing require , passing file in image parameter instead of upload:

params.permit(:image) 

if want keep existing code, have client pass image in field name upload['image']

in rails log, should like

parameters: {"upload"=> { "image" =>#<actiondispatch::http::uploadedfile:0x00000002f0e108 @tempfile=# .....}} 

notice how image inside upload.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -