Rails update has_many through jQuery Draggable Droppable and hidden_field_tag -


i making screen people "vote" each other's favorite images dragging them gallery tray. using jquery draggable / droppable. when user drags image gallery tray, want update has_many :through relationship in database (the vacation votes). trying use hidden_field_tag hold votes.

i not getting parameters passed when form submits. have idea on how this? in advance!

the models

vacation

class vacation < activerecord::base   belongs_to :user   belongs_to :period   has_many :votes   has_many :images, :through => :votes   accepts_nested_attributes_for :votes    attr_accessible :user_id, :period_id, :name end 

vote

class vote < activerecord::base   belongs_to :vacation   belongs_to :image    attr_accessible :image_id, :vacation_id end 

image

class image < activerecord::base   belongs_to :user   belongs_to :category   has_many :votes   has_many :vacations, :through => :votes    attr_accessible :user_id, :title, :facebook_id, :image    validates_presence_of :user_id;   validates_presence_of :title;   validates_presence_of :image;    validates_length_of :title, :maximum => 255    mount_uploader :image, imageuploader end 

the javascript

    var $gallery = $( "#gallery" ),         $tray = $( "#tray_images" );      $( "li", $gallery ).draggable({       cancel: "a.ui-icon",        revert: "invalid",        cursor: "move",       helper: "clone"     });      $tray.droppable({       accept: "#gallery > li",       drop: function( event, ui ) {         moveimagetotray( ui.draggable );         $( "#update_vacation" ).submit();       }     });      function moveimagetotray( $item ) {       $item.fadeout(function() {         $tray.append($item);         $item.fadein();       });     } 

the view /vacations/show.html.erb

<%= form_for @vacation, :html => { :method => :put, :id => "update_vacation" } |f| %>   <%= hidden_field_tag :user_id, @vacation.user_id %>   <%= hidden_field_tag :period_id, @vacation.period_id %>   <%= hidden_field_tag :name, @vacation.name %>    <div id="tray" class="grid_19 push_1 alpha">     <ul id="tray_images" class="gallery">       <% @votes.each |vote| %>         <li class="grid_3 image">           <% link_to @images_all[i] %>             <% content_tag :div %>               <%= image_tag @images_all[i].image_url(:thumb_100_100) %>               <%= hidden_field_tag "vacation[][image_ids]", vote.image_id %>             <% end %>           <% end %>         </li>       <% end %>     </ul>   </div> <% end %>  <div class="clear"></div>    <div class="grid_24"> <ul id="gallery" class="grid_19 push_1 alpha gallery">   <% @images_all.each |image| %>       <li class="grid_3 image">         <% link_to image %>           <% content_tag :div %>             <%= image_tag image.image_url(:thumb_100_100) %>             <%= hidden_field_tag "vacation[image_ids][]", image.id %>           <% end %>         <% end %>       </li>     <% end %>   <% end %> </ul> 

the controller

  def update     params[:vacation][:votes] ||= []     break      @vacation = vacation.find(params[:id])     if @vacation.update_attributes(params[:vacation])       redirect_to @vacation, :notice  => "successfully updated vacation."     else       render :action => 'edit'     end   end 

if want have params form in params[:vacation] hash use:

<%= f.hidden_field :field %> 

instead of

<%= hidden_field_tag :field %> 

also might consider sending data asynchronously instead of submitting form after every drag&drop.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -