Search code examples
javascriptruby-on-railsrubyajaxdata-uri

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!


Solution

  • You get 500 Internal Server Error when permitting image because you don't have an image attribute in the model. You have photo instead, so you should use it as the parameter to send data_uri in the ajax method.

    Change image in the AJAX call to photo:

    data: { picture: { photo: data_uri } }
    

    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