0

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>

Community
  • 1
  • 1
roza
  • 585
  • 1
  • 9
  • 30
  • post your 'new' action too.... 505d80931e7bf2b8150000ac doesn't seems to look like `ids` – Rahul garg Sep 22 '12 at 12:27
  • In posts_controller 'new' I've stadnard only `@post = Post.new`, photos are added asynchronously from Post form by javascript through photos_controller which gives `IDs` like `505d80931e7bf2b8150000ac` from mongodb. This example I think can be done with normal ordinary SQL IDs as '12 ', '13', etc.. – roza Sep 22 '12 at 12:36

1 Answers1

1

If your post has_many pictures mapping,

then your create controller code should be

@post.picture_ids = params[:post][:picture_ids]

instead of

@post.picture_ids = (Picture.find(received_post).id)

Rahul garg
  • 9,202
  • 5
  • 34
  • 67