4

I am using devise-invitable gem in my application. If the user exists in the application and he clicks accept invitation link he should be redirected to sign in page other if new user clicks the link he should be redirected to sign up page. I am not getting how I can override the after_accept_path_for method for that...Where and how can I override this method, can some one please help me in this? Following https://github.com/scambra/devise_invitable/ link

Neha
  • 305
  • 5
  • 23

2 Answers2

7

I think you might want to re-read the documentation, your question is answered in the docs, just not all in one place.

Here are the two sections that concern your question: https://github.com/scambra/devise_invitable#configuring-controllers https://github.com/scambra/devise_invitable#integration-in-a-rails-application

Basically you're going to add a controller for invitations and add routing information for that controller (app/controllers/users/invitations_controller.rb) like this:

class Users::InvitationsController < Devise::InvitationsController
  def after_accept_path_for
    "some path you define"
  end
end

Then you'll change your routes.rb to tell devise to use your invitations controller like:

devise_for :users, :controllers => { :invitations => 'users/invitations' }
trh
  • 7,186
  • 2
  • 29
  • 41
  • 1
    Thanks for your help... Had done the same thing.... but after clicking on the accept invitation link I get redirected to Devise::InvitationsController#edit. Here I am getting the form as set your password, which I don't want .... I want to redirect to signin page if user exists in the application otherwise to signup page...That I am not getting how to do... – Neha Aug 12 '13 at 17:40
  • Can someone help me in this – Neha Aug 13 '13 at 04:16
  • Resolved the issue by overriding the edit action of devise invitable. – Neha Aug 13 '13 at 08:09
1

With devise 2.1.2 and devise_invitable 1.1.8, the page you end up on after setting password from the invitation link depends on what you set the root path for your devise resource in the config/routes.rb so @trh's answer doesn't work with this version (I tried and failed). From the devise code comments:

  # The default url to be used after signing in. This is used by all Devise
  # controllers and you can overwrite it in your ApplicationController to
  # provide a custom hook for a custom resource.
  #
  # By default, it first tries to find a valid resource_return_to key in the
  # session, then it fallbacks to resource_root_path, otherwise it uses the
  # root path. For a user scope, you can define the default url in
  # the following way:
  #
  #   map.user_root '/users', :controller => 'users' # creates user_root_path
  #
  #   map.namespace :user do |user|
  #     user.root :controller => 'users' # creates user_root_path
  #   end
  #
  # If the resource root path is not defined, root_path is used. However,
  # if this default is not enough, you can customize it, for example:
  #
  #   def after_sign_in_path_for(resource)
  #     stored_location_for(resource) ||
  #       if resource.is_a?(User) && resource.can_publish?
  #         publisher_url
  #       else
  #         super
  #       end
  #   end

In my case, I had two different namespaces with one namespace called "portal" which I was using a "PortalUser" class for the user. For Rails 3.2, I simply declared this line in my routes.rb:

get "portal" => 'portal/dashboard#index', as: :portal_user_root

The important note is the naming, "portal_user_root" which is the underscored name of PortalUser class name. Simply setting this named route is all that's needed for your devise_invitable to redirect as desired.

Michael Lang
  • 1,028
  • 12
  • 21