weixin_33743248 2016-08-11 18:37 采纳率: 0%
浏览 36

Rails 4:不允许的参数

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!

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2016-08-11 18:43
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line