Photo uploads using Paperclip with a nested model & strong params

Hi all,

I’m trying to add photo uploads to an Advert model using Strong Params with the Paperclip gem, where Photo is a separate model which “has_attached_file” :upload, and I am calling forms_for (actually semantic_forms_for as I’m using Formtastic) in the Ads#new view to upload the image to the Photo instance. I’m calling accepts_nested_attributes for :photo in the Ad model.

From params it looks as if the upload itself is working ok. The new instance of photo is getting created in the Ads#create action, but I think I’ve missed something in the controller as it is not saving the upload attributes to the new instance. Haven’t been able to get it working despite multiple iterations of the controller code. What would I need to do differently to get it working? Have I screwed something up in the way I’m calling permit on the nested Photo model in the AdsController?

would appreciate any pointers as I haven’t found anything on StackOverflow etc that fully nails this issue of nesting with Strong Params (at least not in a way that has worked for me). I’m using Rails 3.2.13 and Ruby 1.9.3.

Would really appreciate some pointers.

Thanks

Gist

@jawad_uk it looks like you’re missing the flag on semantic_form_for to make the form multipart:

<%= semantic_form_for @ad, :html => { :multipart => true } do |form| %>

This will allow multipart data (in your case, images) to be submitted in the form. The best indicator that the form isn’t wired up as multipart is that the tempfile within the params you’re seeing is an empty array (you should see a Ruby object instance there instead if you’re uploading one file).

Good luck and be sure to post any updates with what you find!