Fernando Aureliano Fernando Aureliano - 2 months ago 25
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

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

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

My Ajax

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

type: 'POST',
url: '/pictures/upload',
async: true,
data: { picture: {photo: data_uri} }

.done(function () {


} );

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!


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