byCoder
byCoder

Reputation: 9184

Route ajax rails trouble

I have such jquery code:

  $(".quantity").blur(function() {
    console.log("upd");
    $.ajax({
    url: "/line_items/update_quantity/",
    type: "GET",
    data: {id: $(this).attr('id'), quantity: $(this).attr('quantity'), cart: $(this).attr('cart')} 
    });
  });

But this code generate me such url:

.../line_items/update_quantity/?id=29&quantity=111&cart=27

But i need such url:

.../line_items/update_quantity/id=28&quantity=2&cart=27

without ?

A have such routes:

match 'line_items/:action/id=:id&quantity=:quantity&cart=:cart' => 'line_items#update_quantity'

I tried, but nothing goes right. Help me please.

def update_quantity
    @cart = current_cart
    @line_item = LineItem.find(params[:id])
    respond_to do |format|
      if @line_item.update_attribute(:quantity, params[:quantity]) #&& @cart.id == params[:cart]
        format.html { redirect_to(@line_item, :notice => 'Line item was successfully updated.') }
        format.js
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @line_item.errors, :status => :unprocessable_entity }
      end
    end
  end

Upvotes: 1

Views: 1704

Answers (2)

ck3g
ck3g

Reputation: 5929

Query parameters should be started after ?

HTTP/1.1: Protocol Parameters

The "http" scheme is used to locate network resources via the HTTP protocol. This section defines the scheme-specific syntax and semantics for http URLs.

http_URL = "http:" "//" host [ ":" port ] [ abs_path [ "?" query ]]

your route can be replaced by

match 'line_items/:action/:id' => 'line_items#update_quantity'

&quantity=:quantity&cart=:cart is unnecessary

or better

resources :line_items do
  get :update_quantity, :on => :member
end

Upvotes: 3

sailor
sailor

Reputation: 8034

You have to append the id at the end of the routes manually:

$(".quantity").blur(function() {
    console.log("upd");
    $.ajax({
    url: "/line_items/update_quantity/" + $(this).attr("id"),
    type: "GET",
    data: {quantity: $(this).attr('quantity'), cart: $(this).attr('cart')} 
    });
  });

But i agree with rubish, you shouldn't update records with a GET url

Upvotes: 1

Related Questions