I need to have from 1 to 15 file fields for photos.
Now I'm uploading files with carrierwave gem building every photo object in my controller like: N.times { @car.photos.build }
and then provide N
file fields for photos in my view.
But N
is static value as you can understand.
How can I generate dynamic count of file fields, like:开发者_如何转开发 1 static from start and add 1 more on every user's ajax request (like user will be clicking Add photo field
link) and dynamically create photo object for it @car.photo.build?
BTW, sorry for my english. Thanks.
You must use javascript to generate dynamic fields. Something like this, using jquery:
var photos_count;
jQuery(function() {
photos_count = $('#photos_fields > field').size();
$('#add_photo').each(function() {
this.onclick = function(event) {
$("#photos_fields").append("<div class='field'><label>Photo</label><input name='car[photos_attributes][" + photos_count + "][image]' type='file'/></div>");
photos_count++;
};
});
});
You can define additional action to ajax creating photos objects, but you can`t build it.
Disclaimer: I'm the author of the post.
See multiple files upload with carrierwave and nested_form. It should help.
精彩评论