我有这样的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
恕我直言,你不应该建立这样的路线。除了进入你已经拥有的小问题之外,你也无法改变参数定义的顺序。 – rubish
@rubish你说的是什么? – byCoder
首先,更新命令应该是PUT/POST,而不是GET。这不是很安全。其次,你需要更好地理解轨道布线,如下所示:http://guides.rubyonrails.org/routing.html – corroded