ruby on rails - Creating associated records inside parent's new view/form. Associated records are done through Ajax/jquery-file-upload -


so have "style" model, has_many "images". in new style page, want able add images in same page. however, since did not create style record yet, not have id. how go collecting images built/uploaded ajax, , updating style_id attributes after saving new style?

i using jquery-file-upload add images image table, , upload files rackspace cloud files. of working, not able set style_id other manually setting it.

is there best practices/proper way go this, since jquery-file-upload uses ajax, not sure best approach saving parent. thinking best approach use ajax submit parent form well, rather use dom, adding hidden inputs/elements parent form?

thank you

style: form partial

<%= form_for @style, :html => {:multipart => true} |f| %>     //normal rails form code here <% end %>  <%= form_for image.new, :html => { :multipart => true, :id => "fileupload"  } |f| %>   <%= f.file_field :file, :multiple => true %> <% end %>  <script> $(function () {   $('#fileupload').fileupload({     datatype: 'json',     url: '<%= style_images_path(1) %>',     add: function (e, data) {         data.context = $('<div class="img uploading"/>').text('uploading...').appendto(document.body);         data.submit();     },     done: function (e, data) {         $.each(data.result.files, function (index, file) {             $('<img src="'+file.thumbnail_url+'">').appendto(data.context);         });         console.log(data);         data.context.append("<span>done</span>");     },     option: {             autoupload: true,     }   }); }); </script> 

in above code can see set style_id manually... style_images_path(1)

-

my proposed solution:

my idea pass array of id's of children (images) create/update method of style. , in controller, update style_id attributes of matching id's newly created style's id... think possible?

that's tricky.

you can instantiate @style within new method of controller. instead than:

@style = style.new 

put

@style = style.create 

if validation complains, use following workaround:

@style = style.new @style.save :validate => false 

now in view have qualified @style id can pass on js:

    url: '<%= style_images_path(@style) %>', 

at point when (if ever) user clicks on form button, control reaches update method, not create (this because _form.html.erb automatically changes http verb post (create) put (update). make sure controller ready this).

you should consider sort of "garbage collection" in case style objects created , never saved. might not straight forward because user can close window , left incomplete style in db. maybe js function triggers @ window close , calls garbage_collect_if_not_saved(@style) method? still not perfect (what if browser hangs?) better nothig. otherwise good-old cron based script cleans db up.

cheers,


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -