It should work but I would recommend using
accepts_nested_attributes_for :image.
Few screencasts:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
Best,
Martin
On Mar 31, 5:22 pm, Jeremy Woertink <li...@ruby-forum.com> wrote:
> ES wrote:
> > I have an object that has multiple photos uploaded to it, using the
> > paperclip plugin. I used a virtual attribute to have mutliple photos
> > in the form but I can't figure out how to get the image to save. It
> > works if I put <%= photo_form.file_field :image_file_name %> instead
> > of just :image. If I use just the following, the image_file_name
> > field is blank when it is saved.
>
> > <% for photo in @analysis.photos %>
> > <% fields_for "analysis[photo_attributes][]", photo do |photo_form|
> > %>
> > <p>
> > image description: <%= photo_form.text_field :description %>
> > <%= photo_form.file_field :image %>
> > </p>
> > <% end %>
> > <% end %>
>
> I know in Rails 2.3.5 there's an easier way to do this, but I cheated a
> bit an used attribute_fu because of this cool helper method.
>
> <% form_for :analysis, @analysis, :url => analysis_path, :html => {
> :multipart => true } do |analysis_form| %>
> <%= analysis_form.render_associated_form(@analysis.photos, :new => 4) %>
> <%= analysis_form.submit('Submit') %>
>
> Then you just add a partial called _photo.html.erb inside your analysis
> view folder, and throw in
>
> <%= analysis_form.label(:image) %>
> <%= analysis_form.file_field(:image) %>
>
> In your model you would put
>
> class Analysis < ActiveRecord::Base
> has_many :photos, :attributes => true
> end
>
> Hope that helps.
>
> ~Jeremy
>
> --
> Posted viahttp://www.ruby-forum.com/.
--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment