I have one post with many pictures. In the one view I have two forms one with standard post fields and another to sending asynchronously pictures to pictures_controller, my JavaScript receives callback only with IDs of new added pictures, not yet associated with parent. How to add received by JavaScript many IDs to hidden field in post form and after sending in post_controller loop over the IDs from the hidden field and associate it with post model? I am trying to do Step 3 (the fun part) from: How to use JQuery-File-Upload to upload multiple images on one page during creating post?.
post view:
= form_for @post do |f|
.field
= f.label :title
= f.text_field :title
.field
= f.label :description
= f.text_area :description
= f.hidden_field :picture_ids
.actions
= f.submit 'Save'
= form_for @picture, :html => { :multipart => true, :id => "fileupload" } do |f|
= f.file_field :image, :multiple => true
%ul.thumbnails.files
JavaScript (I don't know how to add correctly newly received ids to hidden field, on start it contain only: []
):
$('#fileupload').fileupload({
...
completed: function(e, data) {
console.log(data.result[0].picture_id); //e.g. pure: 505e1d811e7bf2b815000139, 505d80181e7bf2b8150000ab etc.
$("#post_picture_ids").val(data.result[0].picture_id);
});
});
post_controller:
def create
@post = Post.new(params[:post])
#@post.picture_ids = params[:post][:picture_ids]
end
post:
{"utf8"=>"✓",
"authenticity_token"=>"FZA1rg53Qc21MCkT4YMDdIrkttmiRmdoGhPC7HS8Fx8=",
"notify"=>{"title"=>"",
"description"=>"",
"picture_ids"=>"505e1d811e7bf2b815000139"},
"commit"=>"Save"}
Mongoid throw error: undefined method 'compact' for #<Picture:0x007faf2513d810>