admin管理员组

文章数量:1242825

I'm working with Devise and DeviseInvitable to manage authentication in my app and I'm having some trouble adding AJAX support to InvitationsController#update. The controller in DeviseInvitable looks like this:

# 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

This works well when resource.errors.empty? == true and we execute:

respond_with resource, :location => after_accept_path_for(resource)

(i.e. invitations/update.js.erb is rendered and my javascript calls are made). The problem is that when resource.errors.empty? == false, and we execute:

respond_with_navigational(resource){ render_with_scope :edit }

the server says:

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

but my javascript calls are not being run. Can anyone explain what respond_with_navigational is supposed to be doing? I've been googling for hours and I haven't found an explanation of this api anywhere.

Thanks!

I'm working with Devise and DeviseInvitable to manage authentication in my app and I'm having some trouble adding AJAX support to InvitationsController#update. The controller in DeviseInvitable looks like this:

# 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

This works well when resource.errors.empty? == true and we execute:

respond_with resource, :location => after_accept_path_for(resource)

(i.e. invitations/update.js.erb is rendered and my javascript calls are made). The problem is that when resource.errors.empty? == false, and we execute:

respond_with_navigational(resource){ render_with_scope :edit }

the server says:

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

but my javascript calls are not being run. Can anyone explain what respond_with_navigational is supposed to be doing? I've been googling for hours and I haven't found an explanation of this api anywhere.

Thanks!

Share Improve this question asked Jul 21, 2011 at 0:46 spinlockspinlock 3,9475 gold badges37 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17

OK, I figure out what respond_with_navigational is doing. It's defined in the Devise base classes as such:

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

and, navigational_formats is defined in Devise as well:

# 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

So, it's basically a wrapper for respond_with(). In order to get this to work, I had to add the following to my InvitationsController:

respond_to :html, :js

and now, update.js.erb is being rendered properly.

本文标签: javascriptHow does quotrespondwithnavigationalquot workStack Overflow