Rails Devise Invitable redirect after send invitit

2019-02-09 18:14发布

问题:

I got the devise invitable installed and working. Trying to figure out how to redirect the user after he/she sent an invitation out. Right now it's redirecting me to the root. I thought you can just set your custom path in the method below but it didn't work. Thanks in advance if anyone know where to customize the path after invite sent.

 def after_invite_path_for(resource)
    new_profile_path
  end

回答1:

I stumbled upon your question because I was having the same issue. As far as I can tell the intended way for you to override after_invite_path_for is to override Devise::InvitationsController.

class Users::InvitationsController < Devise::InvitationsController
  def after_invite_path_for(resource)
    new_profile_path
  end
end

routes.rb

devise_for :users, :controllers => { :invitations => "users/invitations" }

It would be nice if devise invitable worked like devise proper and you could override its after invite/accept paths in application controller. I modified devise_invitable to work that way and submitted a pull request. I'm not sure if it will be accepted or not, but you can have a look here: https://github.com/scambra/devise_invitable/pull/240.

If that feature is accepted, you could patch your current version of invitable to respect definitions of after invite/accept paths in application controller by putting this in an initializer:

#make invitable's path functions overridable in application controller
[:after_invite_path_for, :after_accept_path_for].each do |method|
  Devise::InvitationsController.send(:remove_method, method) if ApplicationController.method_defined? method
end


回答2:

Not sure if its a good thing... or the worse to do however you can put:

  def after_invite_path_for(resource)
    new_profile_path
  end

in your application controller... seems to work OK!