# Attachment\_fu and nested model forms

**URL:** <https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522>\
**Category:** Rails\
**Created:** [February 27, 2009, 3:36am UTC](https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522 "2009-02-27T03:36:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [February 27, 2009, 3:36am UTC](https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522/1 "2009-02-27T03:36:18Z")

</div>

Has anyone successfully used attachment\_fu with rails 2.3 nested model  
forms?

I’ve never used attachment\_fu before so I may be doing something wrong  
there not sure. Here’s what I’ve got:

class Product \< ActiveRecord::Base  
has\_one :cover\_image  
accepts\_nested\_attributes\_for :cover\_image, :allow\_destroy =\> true  
end

class CoverImage \< ActiveRecord::Base  
has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:path\_prefix =\> 'public/images/covers  
end

standard rest controller action:  
def create  
@product = Product.new(params[:product])

```
respond_to do |format|
  if @product.save
    flash[:notice] = 'Product was successfully created.'
    format.html { redirect_to(@product) }
    format.xml { render :xml => @product, :status

```

=\> :created, :location =\> @product }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @product.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

and the new form:

\<% form\_for(:product, :url =\> products\_path, :html =\> { :multipart =\>  
true }) do |f| %\>  
…  
…  
\<% f.fields\_for(:cover\_image) do |c| %\>  
\<%= c.file\_field :uploaded\_data %\>  
\<% end %\>  
…  
…  
\<% end %\>

On submitting the form I get this error:  
ActiveRecord::AssociationTypeMismatch (CoverImage(#70223327600060)  
expected, got HashWithIndifferentAccess(#70223341431440)):

Any thoughts greatly appreciated!  
Tim

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [February 27, 2009, 6:44am UTC](https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522/2 "2009-02-27T06:44:25Z")

</div>

On Thu, Feb 26, 2009 at 9:35 PM, Tim [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Has anyone successfully used attachment\_fu with rails 2.3 nested model  
> forms?

Yes.

> ```
> :storage => :file_system,
> :path_prefix => 'public/images/covers
> 
> ```
> 
> end

Maybe you just omitted this, but I think your CoverImage should declare  
belongs\_to :product

standard rest controller action:

> ```
> format.html { render :action => "new" }
> 
> ```
> 
> …  
> …  
> \<% f.fields\_for(:cover\_image) do |c| %\>  
> \<%= c.file\_field :uploaded\_data %\>  
> \<% end %\>  
> …  
> …  
> \<% end %\>

Since you have @product in your controller, why not just use that in  
form\_for? Doing so will allow you to omit :url =\> products\_path. Thus,  
you’d  
have

\<% form\_for(@product, :html =\> { :multipart =\> true }) do |f| %\>

> On submitting the form I get this error:  
> ActiveRecord::AssociationTypeMismatch (CoverImage(#70223327600060)  
> expected, got HashWithIndifferentAccess(#70223341431440)):
> 
> Any thoughts greatly appreciated!

I don’t know if any of that solves your problem, but that’s how I’d do  
it.  
Also, I relied on Ryan Daigle’s blog post when I was doing nested  
models/forms. Check it out:

[http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes](http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes)

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [February 27, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522/3 "2009-02-27T18:02:55Z")

</div>

> Maybe you just omitted this, but I think your CoverImage should declare  
> belongs\_to :product

Thanks for the idea Craig. Thought we were on to something there for  
a second but apparently not.

The update seems to work just not the create. If I change the create  
method to this: (yes I changed my image class and relationship names)

def create  
params[:product][:image] = ProductImage.new(params[:product]  
[:image])  
@product = Product.new(params[:product])

```
respond_to do |format|
  if @product.save
    flash[:notice] = 'Product was successfully created.'
    format.html { redirect_to(admin_products_path) }
    format.xml { render :xml => @product, :status

```

=\> :created, :location =\> @product }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @product.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

it works fine and the update works as expected with a standard REST  
action.

I also noticed that if I change the image relationship to has\_many the  
form fields aren’t getting indexed so I end up with this:

instead of this:  
  
etc…

I dunno seems like there must be something really fundamental that I’m  
missing.

Help:-)!

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [October 19, 2009, 4:53am UTC](https://www.ruby-forum.com/t/attachment-fu-and-nested-model-forms/161522/4 "2009-10-19T04:53:55Z")

</div>

I found your post and was having the same issue. I was able to solve it  
by adding “\_attributes” to the nested model in the form:

\<% form\_for(:product, :url =\> products\_path, :html =\> { :multipart =\>  
true }) do |f| %\>  
…  
…  
\<% f.fields\_for(:cover\_image\_attributes) do |c| %\>  
\<%= c.file\_field :uploaded\_data %\>  
\<% end %\>  
…  
…  
\<% end %\>

For some reason, Rails, or attachment\_fu are missing that link. I wrote  
a post about it

> **[Nested Model Forms and attachment\_fu](https://brandon-harris.com/2009/10/18/nested-model-forms-and-attachment-fu/)**
>
> I think that a lot of Rails developers have moved on to Paperclip . Most of us started with attachment\_fu , and I am willing to wager that we still have projects floating around using attachment\_fu.
> I updated one of these older projects to 2.3.4, and...

.

> I dunno seems like there must be something really fundamental that I’m  
> missing.
