路线阿贾克斯导轨麻烦 [英] Route ajax rails trouble

查看:129
本文介绍了路线阿贾克斯导轨麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的jQuery code:

  $(数量)。模糊(函数(){
    的console.log(UPD);
    $阿贾克斯({
    网址:/ line_items / update_quantity /,
    键入:GET,
    数据:{ID:$(本).attr('身份证'),数量:$(本).attr('数量'),购物车:$(本).attr('购物车')}
    });
  });
 

但是,这code产生了我这么网址:

  

... / line_items / update_quantity / ID = 29&安培;数量= 111安培;车= 27

不过,我需要这样的网址:

  

... / line_items / update_quantity / ID = 28&安培;数量= 2及车= 27

不?

一个有这样的路线:

  

匹配line_items /:动作/ ID =:标识和放大器;数量=:数量&安培;车=:购物车'=>   line_items#update_quantity

我试过了,但没有去正确的。请帮助我。

 高清update_quantity
    @cart = current_cart
    @line_item = LineItem.find(PARAMS [:ID])
    respond_to代码做|格式|
      如果@ line_item.update_attribute(:数量,则params [:数量])#&功放;&安培; @ cart.id == PARAMS [:车]
        的format.html {redirect_to时(@line_item,:通知=>线项目已成功更新。')}
        format.js
        format.xml {头:OK】
      其他
        的format.html {渲染:行动=> 编辑 }
        format.xml {渲染:XML => @ line_item.errors,:状态=> :unprocessable_entity}
      结束
    结束
  结束
 

解决方案

查询参数应在

启动

HTTP / 1.1协议参数

  

在HTTP方案用于通过HTTP协议来定位网络资源。本节定义方案的具体语法和语义的HTTP URL。

     

HTTP_URL =HTTP://主持人[:口] [绝对路径[?查询]]

你的路由可以被替换为

 匹配line_items /:动作/:身份证'=> line_items#update_quantity
 

&放大器;数量=:数量&安培;车=:车不需要

或更好

 资源:line_items做
  得到:update_quantity,:上=> :会员
结束
 

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

解决方案

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

这篇关于路线阿贾克斯导轨麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆