On Mar 20, 2013, at 9:04 AM, avinash behera wrote:
Upload is fine.
Suppose I am uploading five images, which I can do with file upload.
But I also need to upload those images to a specific category. I also need to
save Image name and which category(dropdown) it belongs into the db.
Suppose I uploaded 2 images:-
After clicking on submit, I will get :-
Image , Image Name : Text(We have to input), Category : Dropdown ---- It
will repeat
Are you trying to assign these attributes after the file is uploaded, or
at the same time that you upload it? I usually follow this pattern
(pseudocode!!):
class Article < ActiveRecord::Base
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images
…
end
class Image < ActiveRecord::Base
belongs_to :article
…
end
Image can have all the unique attributes – filename, uuid, whatever
else is needed by your chosen attachment library (Dragonfly, Paperclip,
CarrierWave) along with the category, caption, anything else you need in
your application.
Using Ryan B.’ nested_form Action, you can add as many of these as
you want at the time you create or edit the parent object. I recommend a
quick view of the associated RailsCast:
#196 Nested Model Form Part 1 - RailsCasts and
#197 Nested Model Form Part 2 - RailsCasts
The upshot of this construction is that you can attach as many rich
(decorated) children to the parent as you need, and each one will be
fully populated in a single form submission. You won’t need to come back
and edit the children to add the metadata to the file later (although
you can if you really want to).
Walter