Rails form_for 忽略 ":remote =>真实的“”第一次 ajax 调用后
好吧,伙计们,这个问题已经让我沮丧了几个小时了。我已经搜索过,似乎找不到其他人有类似的问题。任何帮助将不胜感激。
我正在尝试对我的控制器进行 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论