views:

376

answers:

1

Im having a problem transferring an SQLlite Rails 3 app over to a Mongoid Rails 3 app. In the SQLlite version, I am easily able to include an image upload form (using Paperclip) from one model ('image') within a nested form from another model ('product'). Here's my 'new' product form:

  <%= form_for @product, :html => {:multipart => true} do |f| %>
     <% f.fields_for :images do |image_form| %>
       <%= f.label :productphoto %>
       <%= f.file_field :productphoto %><br />
    <% end %>
 <% end %>

And here's the 'show' view:

    <% @product.images.each do |image| %>
      <%= image_tag image.productphoto.url(:gallerythumb) %><br />
    <% end %>

When I try to use the same product views in my Mongoid Rails 3 app (using Carrierwave), I get the following error:

    TypeError in Stores#show: 
    can't convert nil into String
    <%= image_tag product.image.url(:gallerythumb) %>

Im pretty sure my models in the Mongoid version are correct because if I add a string (like 'name') to my 'image' model and nest that in the 'Product' form, it works. Also, Im able to upload an image into a non-nested model form.

Any help would be greatly appreciated!

A: 

I just had a similar problem myself. The problem is not the image upload I think, but the problem is that Rails doesn't recognize :images as being an Array. If you look into the Rails source of the fields_for helper you see that it checks for a method "_attributes=". If that's not there the form will be posted as normal fields and not as an array (params will be "images" instead of "images[0]")

You have to add the following line to your model:

accepts_nested_attributes_for :images
Jeroen van Dijk