weixin_33693070 2012-05-11 13:17 采纳率: 0%
浏览 15

路由ajax rails麻烦

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
  • 写回答

2条回答 默认 最新

  • weixin_33701294 2012-05-11 15:35
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题