javascript - How to pass attributes from jQuery to Rails using ajax -


i'm trying implement rails 3.2 drag , drop shopping cart using jquery ($.ajax).

problem: can't figure out how pass line_items controller :product_id option in javascript. believe ajax key, that's heads @ right now. currently, done in html using button_to embedded ruby so:

(<%= button_to('add cart', line_items_path(:product_id => product), :remote => true) %>) 

however implement drag/drop function, above action needs triggered upon drop. how can done in javascript?

my controller:

# post /line_items # post /line_items.json def create      @cart = current_cart      product = product.find(params[:product_id])      @line_item = @cart.add_product(product.id)       respond_to |format|      if @line_item.save      format.html { redirect_to(store_index_url) }      format.js   { @current_item = @line_item }      format.json { render json: @line_item, status: :created, :location => @line_item }      else      format.html { render :action => "new" }      format.json { render json: @line_item.errors, status: :unprocessable_entity }     end    end end 

my html:

    <% @products.each |product| %>      <div class="entry" data-id="<%= product.id %>">        <h3><%= product.title %> </h3>        <%= image_tag(product.image_url)%>        <div style="display: none; width: 10px"><%= sanitize(product.description) %>   </div>        <div class="price_line">          <span class="price"><%= number_to_currency(product.price) %></span>(<%= product.unit_of_measure%>)      <br>      <%= button_to('add cart', line_items_path(:product_id => product), :remote => true) %>    </div>  </div>    <p> <%  end %> 

my javascript:

    $(document).ready(function(){     //end navigation buttons     var dragdrop;  $('.entry').draggable({     revert: true ,     revertduration: 500,     containment: "document",      zindex: 100,      helper: "clone",     start: function(){         dragdrop = $(this).data('id');     } });  $('#cart').droppable({     accept: '.entry',     activeclass: 'active',     hoverclass: 'hovered',     drop: function( event, ui ){         $.post({            url: "<%= escape_javascript(line_items_path(:product_id => product)) %>"         });     } }); ... 

so far drag works , drop works except there no :product_id passed line_items. error bad uri '/store/[object object]'. in command console when drop item.

the post function should be:

$.post({    url: "<%= escape_javascript(line_items_path(:product_id => product.id)) %>" }); 

note product.id instead of product


Comments