I have a User model, and the Volunteer model that inherits from the User model:
class User < ActiveRecord::Base
end
class Volunteer <
perhaps a little to late but you can try the following in your routes.rb
devise_for :users , :skip => :registrations
as :user do
match "volunteers/edit(.:format)", :to => "devise/registrations#edit"
end
devise_for :volunteers , :skip => :sessions
The above code assumes that all users and its subclasses (assuming what you are implementing STI to achieve Users model hierarchy) can sign_in and sign_out, but only volunteers can register. Since a volunteer is a user, the user should be able to edit his registration as such. You can add more routes inside the as :user block.
I faced similar problem and solved it like this. But is this solution is specific for cancan gem.
application_controlller.rb
def actual_user
@actual_user ||= current_user.present? ? current_user : current_volunteer
end
def current_ability
@current_ability ||= Ability.new(actual_user)
end
I think this way you allow a user to be signed in as a User and a Voluteer in the same session.
Once you have a way to deal with that could you not just have
# in application_controller.rb
alias_method :devise_current_user, :current_user
def current_user
devise_current_user || current_volunteer
end
in your application_controller.rb
I have accepted viktor tron's answer beacuse it seems the cleanest method.
However, i have resolved my issue in a different way.
I've ended up hardcoding the sign in process for other classes as :user
. This gives me access to the current_user
method even for the volunteer class.
class Volunteers::SessionsController < Users::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
if resource
flash[:notice] = "You are logged in"
sign_in(:user, resource)
super
else
super
end
end
end