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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Feb 26, 2009 at 9:35 PM, Tim <mcintyre.tim-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Has anyone successfully used attachment_fu with rails 2.3 nested model > forms?Yes.> 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 > endMaybe you just omitted this, but I think your CoverImage should declare belongs_to :product 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 %>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 Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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: <input id="product_image_uploaded_data" name="product[image] [uploaded_data]" size="30" type="file" /> instead of this: <input id="product_image_0_uploaded_data" name="product[image][0] [uploaded_data]" size="30" type="file" /> etc... I dunno seems like there must be something really fundamental that I''m missing. Help:-)! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 http://brandon-harris.com/2009/10/18/nested-model-forms-and-attachment-fu .> I dunno seems like there must be something really fundamental that I''m > missing.-- Posted via http://www.ruby-forum.com/.