【问题标题】:respond_to not working with format.js if format.html is present如果 format.html 存在,respond_to 不能与 format.js 一起使用
【发布时间】:2011-01-27 20:15:32
【问题描述】:

使用respond_to 时,我的 AJAX 调用仅在 HTML 响应不存在时调用 javascript 响应。

我有以下 javascript 用于提交就地编辑操作:

$(".eip").editable("/surveys", {
  name   : 'survey[name]',
  select : true,
  event  : 'edit',
  submit : 'Save',
  cancel : 'cancel',
  cssclass : "editable",
  onblur : 'ignore',
  onedit : function() { $(".eip_desc").hide(); },
  onreset : function() { $(".eip_desc").show(); },
  onsubmit : function() { $(".eip_desc").show(); }
});

然后我有以下调查控制器方法:

class SurveysController < ApplicationController
  def create
    @survey = Survey.new(params[:survey])    

    if @survey.save
      respond_to do |format|
        format.html { redirect_to surveys_path, :notice => "Survey created!" }
        format.js
      end
    else
      render :action => :new
    end
  end
end

如果我删除了format.html 行,那么format.js 会做出应有的响应。但是,如果我将format.html 留在其中,那么当我通过该editable javascript 位提交时,整个页面都会呈现。

我正在为此应用运行 Rails 3.0.3。

【问题讨论】:

  • 正确的操作可能是 UPDATE 吗?您正在编辑资源吗?

标签: javascript ruby-on-rails respond-to


【解决方案1】:

来自文档

(String) 方法:何时使用的方法 提交编辑的内容。默认为 邮政。您很可能想使用 POST 或放置。 PUT 方法兼容 导轨。

尝试传递一个方法:“put”

【讨论】:

  • 是正确的(题外话:但 PUT 可以(以正确的方式)用于创建或更新。如果您知道如何命名您的资源 PUT 可能是正确的 coiche)。在这种情况下可能是操作错误,更新操作必须提供在线编辑。对不起我的英语
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多