2011-07-21 50 views
6

我与制定和DeviseInvitable工作在我的应用程序来管理身份验证,我遇到了一些麻烦增加InvitationsController#更新AJAX的支持。在DeviseInvitable控制器看起来是这样的:“respond_with_navigational”是如何工作的?

# 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) 

(即邀请/ 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应该做什么?我一直在Google上搜索几个小时,并且在任何地方我都没有找到这个API的解释。

谢谢!

回答

10

OK,我找出respond_with_navigational在做什么。它在设计基类定义为这样:

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

,并navigational_formats在设计定义,以及:

# 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被正确地呈现。

+0

来到这里是因为这个答案在这里:http://stackoverflow.com/a/9154096/18706(万一有人在寻找使用它的一个实际的例子)。 – mahemoff

+0

此外,这是默认初始值设定项中的说明:https://gist.github.com/3824340 – mahemoff