真实的“”第一次 ajax 调用后" />

Rails form_for 忽略 ":remote =>真实的“”第一次 ajax 调用后

发布于 2024-12-20 10:00:21 字数 1745 浏览 2 评论 0 原文

好吧,伙计们,这个问题已经让我沮丧了几个小时了。我已经搜索过,似乎找不到其他人有类似的问题。任何帮助将不胜感激。

我正在尝试对我的控制器进行 AJAX 调用来更新 line_item 的数量属性。代码如下:

###Controller update action###

def update
  @line_item = LineItem.find(params[:id])
  @quote = @line_item.quote
  respond_with(@line_item, @quote) do |format|
    if @line_item.update_attributes(params[:line_item])
      format.html  { redirect_to(@quote,
                    :notice => 'Quantity was successfully updated.')}
      format.js
    else
      format.html  { redirect_to(@quote,
                    :notice => 'Oops!  James messed up again.')}
      format.js  { render 'shared/update_error' }
    end
  end
end

#####View code######

- @quote.line_items.each do |item|
    = form_for item, :remote => true do |f|
      %tr
        %td.quantity_cell
          = f.text_field :quantity, :size => 2
          = image_submit_tag("update_quantity_button.png", :id => "quantity_update")

####Javascript for the callback####

$("#quote_show_parts").html("<%= escape_javascript(render 'shared/show_quote') %>");

问题是第一个请求将按预期工作。调用被执行,记录被更新,并且更改反映在页面上。检查页面源码,可以看到表单中存在“_method ... 'put'”隐藏标签。

如果我更改字段中的值并再次单击按钮,我会收到路由错误:

Started POST "/line_items/90" for 127.0.0.1 at 2011-12-08 15:11:45 -0500

ActionController::RoutingError (No route matches [POST] "/line_items/90"):

现在,似乎 _method 标记(在检查页面源代码时仍然存在)被忽略并进行了 POST 调用。无论哪种方式,都会为该 URL 定义 POST 路由,因此我不确定为什么会发生“无路由”错误。如果我删除 ':remote => true' 功能按预期工作,没有任何问题。

非常感谢大家。

编辑:添加模型代码:

class LineItem < ActiveRecord::Base
  belongs_to :part
  belongs_to :quote

  def total_price(company)
    part.price(company) * quantity
  end
end

OK guys this one has had me frustrated for a few hours now. I've searched and can't seem to find anyone else having a similar issue. Any help would be much appreciated.

I am trying to make an AJAX call to my controller to update a line_item's quantity property. The code is as follows:

###Controller update action###

def update
  @line_item = LineItem.find(params[:id])
  @quote = @line_item.quote
  respond_with(@line_item, @quote) do |format|
    if @line_item.update_attributes(params[:line_item])
      format.html  { redirect_to(@quote,
                    :notice => 'Quantity was successfully updated.')}
      format.js
    else
      format.html  { redirect_to(@quote,
                    :notice => 'Oops!  James messed up again.')}
      format.js  { render 'shared/update_error' }
    end
  end
end

#####View code######

- @quote.line_items.each do |item|
    = form_for item, :remote => true do |f|
      %tr
        %td.quantity_cell
          = f.text_field :quantity, :size => 2
          = image_submit_tag("update_quantity_button.png", :id => "quantity_update")

####Javascript for the callback####

$("#quote_show_parts").html("<%= escape_javascript(render 'shared/show_quote') %>");

The problem is that the first request will work as expected. the call is executed, record updated, and the change reflected on the page. Checking the page source, you can see the "_method ... 'put'" hidden tag in the form.

if i change the value in the field and hit the button again, i get a routing error:

Started POST "/line_items/90" for 127.0.0.1 at 2011-12-08 15:11:45 -0500

ActionController::RoutingError (No route matches [POST] "/line_items/90"):

Now, it seems as though the _method tag (still there when checking the page source) is being ignored and a POST call being made. Either way a POST route is defined for that URL so i'm not sure why the "no route" error would happen. if i remove the ':remote => true' the functionality works as expected and there are no issues.

Thank you all very much is advance.

EDIT: added model code:

class LineItem < ActiveRecord::Base
  belongs_to :part
  belongs_to :quote

  def total_price(company)
    part.price(company) * quantity
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文