Fernando Aureliano Fernando Aureliano - 3 months ago 47
Ruby Question

Rails 4: Unpermitted parameter

I have a model Picture where I take a photo with webcam and save it. I upload the images with carrierwave, and generate the image with the gem carrrierwave-data-uri The image is sent by ajax

the function on my PicturesController.rb is:

def upload_image
photo = params[:picture][:photo]
@picture = Picture.new(picture_params)
@picture.user_id = current_user.id
@picture.photo = photo
@picture.save
end


private
def set_picture
@picture = Picture.find(params[:id])
end

def picture_params
params.require(:picture).permit(:photo, :comment, :user_id )
end
end


My Ajax

function save_photo() {
Webcam.snap( function(data_uri) {

$.ajax({
type: 'POST',
url: '/pictures/upload',
async: true,
data: { picture: {photo: data_uri} }
})

.done(function () {
console.log(photo);
});

Webcam.reset();

} );
}


When I send the image, it is registered on database, but not the image. I getting the console error:

Unpermitted parameter: image

But if I add the parameter :image on my strong parameters, I get the Completed 500 Internal Server Error

The data uri is printed on my console, but I can't put it in a image variable

Thanks by help!

Answer

Change image in ajax code to photo like this

data: { picture: {photo: data_uri} }

You get 500 server error when permitting image because you dont have image attribute in model but photo attribute. So you should use photo as parameter to send data_uri in ajax method. Also make change in your controller method like this:

  def upload_image 
    @picture = Picture.new(picture_params)
    @picture.user_id = current_user.id 
    @picture.save
  end