javascript - "respond_with_navigational"是如何工作的?

标签 javascript jquery ajax ruby-on-rails-3

我正在使用 Devise 和 DeviseInvitable 来管理我的应用程序中的身份验证,但我在向 InvitationsController#update 添加 AJAX 支持时遇到了一些问题。 DeviseInvitable 中的 Controller 如下所示:

# invitations_controller.rb

# PUT /resource/invitation                                                                                                 
def update
  self.resource = resource_class.accept_invitation!(params[resource_name])

  if resource.errors.empty?
    set_flash_message :notice, :updated
    sign_in(resource_name, resource)
    respond_with resource, :location => after_accept_path_for(resource)
  else
    respond_with_navigational(resource){ render_with_scope :edit }
  end
end

这在 resource.errors.empty 时有效吗? == true 然后我们执行:

respond_with resource, :location => after_accept_path_for(resource)

(即呈现 invitations/update.js.erb 并进行我的 javascript 调用)。问题是什么时候resource.errors.empty? == false,我们执行:

respond_with_navigational(resource){ render_with_scope :edit }

服务器说:

Rendered invitations/update.js.erb (1.4ms)

但我的 javascript 调用没有运行。谁能解释一下 respond_with_navigational 应该做什么?我已经在谷歌上搜索了几个小时,但我没有在任何地方找到这个 api 的解释。

谢谢!

最佳答案

好的,我知道 respond_with_navigational 在做什么。它在 Devise 基类中定义如下:

def respond_with_navigational(*args, &block)
    respond_with(*args) do |format|
      format.any(*navigational_formats, &block)
    end
end

并且,navigational_formats 也在 Devise 中定义:

# Returns real navigational formats which are supported by Rails
def navigational_formats
    @navigational_formats ||= Devise.navigational_formats.select{ |format| Mime::EXTENSION_LOOKUP[format.to_s] }
end

因此,它基本上是 respond_with() 的包装器。为了让它工作,我必须将以下内容添加到我的 InvitationsController 中:

respond_to :html, :js

现在,update.js.erb 正在正确呈现。

关于javascript - "respond_with_navigational"是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6770193/

相关文章:

javascript - 无法删除空格(在使用 lettering.js 拆分 div 之后)

javascript - angular.js 中的过滤数组

javascript - 创建一个计时器,它会在每次单击按钮后自动重新启动,并且当前使用该站点的所有用户都可以看到相同的信息

javascript - 使用 $ajax 对象重新运行 ajax 调用?

javascript - 使用 XMLHttpRequest.responseText 解析 XML 文件不会显示在 div 上

javascript - onclick vue js中的多个元素

javascript - 打开选项卡中的链接重定向到登录页面而不是给定的 url

jQuery 函数未在加载页面上启动

JQuery parseJSON Tweet ID 汇总问题和解决方案

javascript - 使用 JavaScript 和 AJAX 从 HTML 输入标记检索图像并将其传递给 PHP 脚本以上传到数据库