路由ajax rails麻烦

时间:2012-05-11 13:17:32

标签: jquery ruby-on-rails ajax

我有这样的jquery代码:

  $(".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

没有?

有这样的路线:

  

匹配'line_items /:action / id =:id& quantity =:quantity& cart =:cart'=>   '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 个答案:

答案 0 :(得分:3)

查询参数应在?

之后启动

HTTP/1.1: Protocol Parameters

  

“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

答案 1 :(得分: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网址更新记录