Multiple file attachment for a model in a same form (nested form)
I have a model (say 'X') which can have ma开发者_运维百科ny submodel (say model 'Y', where 'Y' has has a title, description and a file attachment). I have implemented Y with paperclip for file attachment. How do I create a form for model 'X' which contains a form for the sub model 'Y' and is inside the form 'X', moreover i need to be able to associate multiple 'Y' objects with 'X' (like attaching multiple files in a mail in gmail or yahoo)? Is there any gem for this purpose?
Thanks, RoR n00b.
Below you'll find a simple example of what you're looking for, and in the example I've changed your 'X' model to 'Foo' and 'Y' to 'Bar'.
Model:
class Foo
has_many :bars
accepts_nested_attributes_for :bars
end
View:
<%= form_for @foo do |foo_form| %>
<%= foo_form.text_field :blah %>
...
<%= fields_for @foo.bars do |bar_fields| %>
<%= bar_fields.file_field :photo%>
...
<% end %>
<% end %>
精彩评论