Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload with Rails 4, Strong Parameters and Carrierwave

I am porting an application that uses Carrierwave to Rails 4, but I have problems with strong params. I have a model with

accepts_nested_attributes_for :photos

Here is how uploaded images are passed:

{
    # ...
    "model"=>
    {
        # ...
        "photos_attributes"=>
        {
            "1362752177921"=>
            {
                "image"=>"test.jpg",
            }
        }
    }
}

However I can't seem to figure out how to write parameters that will accept photos_attributes.

I have tried .permit(photos_attributes: []) but it simply skips them, when I use permit!, uuid that is created before saving doesn't appear in SQL and this is the second issue:

photos.uuid may not be NULL: INSERT INTO "photos" ("created_at", "model_id", "image", "title", "updated_at") VALUES (?, ?, ?, ?, ?)

Documentation for strong parameters is lacking here and I am not even sure how to proceed.

Update This worked with the nested attributes:

params.permit( ..., :photos_attributes => ['id', 'title', 'image', '_destroy'])

But looks like either Carrierwave or Nested Form should be updated for Rails 4 first. It simply tries to save an empty image all the time. Same code (without strong_params) works in Rails 3.

like image 298
firedev Avatar asked Mar 08 '13 15:03

firedev


1 Answers

This worked with the nested attributes:

params.permit( ..., :photos_attributes => ['id', 'title', 'image', '_destroy'])

But looks like either Carrierwave or Nested Form should be updated for Rails 4 first. It simply tries to save an empty image all the time which I kinda eliminated with :reject_if, but still it is not 100 working.

like image 172
firedev Avatar answered Oct 11 '22 10:10

firedev