"respond_with_navigational"如何运作?

spi*_*ock 6 javascript ajax jquery ruby-on-rails-3

我正在使用Devise和DeviseInvitable来管理我的应用程序中的身份验证,我在向InvitationsController#update添加AJAX支持时遇到了一些麻烦.DeviseInvitable中的控制器如下所示:

# 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
Run Code Online (Sandbox Code Playgroud)

resource.errors.empty? == true我们执行时,这很有效:

respond_with resource, :location => after_accept_path_for(resource)
Run Code Online (Sandbox Code Playgroud)

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

respond_with_navigational(resource){ render_with_scope :edit }
Run Code Online (Sandbox Code Playgroud)

服务器说:

Rendered invitations/update.js.erb (1.4ms)
Run Code Online (Sandbox Code Playgroud)

但我的javascript调用没有运行.任何人都可以解释应该做什么respond_with_navigational吗?我一直在谷歌搜索几个小时,我没有在任何地方找到这个api的解释.

谢谢!

spi*_*ock 11

好的,我弄明白在respond_with_navigational做什么.它在Devise基类中定义如下:

def respond_with_navigational(*args, &block)
    respond_with(*args) do |format|
      format.any(*navigational_formats, &block)
    end
end
Run Code Online (Sandbox Code Playgroud)

并且,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
Run Code Online (Sandbox Code Playgroud)

所以,它基本上是一个包装respond_with().为了使这个工作,我不得不将以下内容添加到我的InvitationsController:

respond_to :html, :js
Run Code Online (Sandbox Code Playgroud)

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

  • 来到这里是因为这个答案:http://stackoverflow.com/a/9154096/18706(以防有人正在寻找使用它的实际示例)。 (2认同)