2012-05-11 52 views
1

我有这样的jQuery代码:路线AJAX轨麻烦

$(".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')} 
    }); 
    }); 

但这个代码生成我这样的网址:

.../line_items/update_quantity/ID = 29 &量= 111 &车= 27

但我需要这样的网址:

.../line_items/update_quantity/ID = 28 &量= 2 &购物= 27

无?

A具有这样的路线:

匹配 'line_items /:动作/ ID =:ID &量=:量&购物=:购物车'=> 'line_items#update_quantity'

我试过了,但没有任何结果。请帮帮我。

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 
+2

恕我直言,你不应该建立这样的路线。除了进入你已经拥有的小问题之外,你也无法改变参数定义的顺序。 – rubish

+0

@rubish你说的是什么? – byCoder

+1

首先,更新命令应该是PUT/POST,而不是GET。这不是很安全。其次,你需要更好地理解轨道布线,如下所示:http://guides.rubyonrails.org/routing.html – corroded

回答

3

查询参数应后?

HTTP/1.1: Protocol Parameters

启动0

“http”方案用于通过HTTP协议定位网络资源。本节为http URL定义特定于方案的语法和语义。

http_URL =“http:”“//”host [“:”port] [abs_path [“?”查询]

路线可以通过

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

&quantity=:quantity&cart=:cart更换是不必要的

或更好

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

然后如何发送这个参数到方法?你没有理解我想要什么 – byCoder

+0

比它给了我这样的链接update_quantity/28?quantity = 2&cart = 27因为如果我做了如下的jscript http://192.168.1.4:3000/line_items/update_quantity/?id=28&quantity = 3&cart = 27它给我找不到ID = update_quantity – byCoder

+0

是的LineItem。你的行为是否被执行? – ck3g

1

你的id在路线的终点手动附加:

$(".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')} 
    }); 
    }); 

但我rubish同意,你不应该用GET URL更新记录