开发者

devise and STI how to login as Base class upon registration

开发者 https://www.devze.com 2023-03-31 14:44 出处:网络
I\'ve seen similar posts already, but couldn\'t quite get the answer I needed. I have a User model and using STI a Student model that is a type of User.

I've seen similar posts already, but couldn't quite get the answer I needed.

I have a User model and using STI a Student model that is a type of User.

When I create a new Student, Devise logs in that Student with a student_session. The problem is the rest of my app uses a user_session. SO, should I create a new user_session using the student_session? and then logout the student?

Or is there a way to get Devise to allow a student creation, but login as the 开发者_如何学运维User base model?

Thank you, Anthony


Check out this post and see if it helps you:

Rails: Using Devise with single table inheritance

The summary is essentially to do the following:

config/routes.rb:

devise_for :users, :controllers => { :sessions => 'sessions' }, :skip => :registrations
devise_for :students, :skip => :sessions

app/controllers/sessions_controller.rb:

class SessionsController < Devise::SessionsController
    def create
        rtn = super
        sign_in(resource.type.underscore, resource.type.constantize.send(:find, resource.id)) unless resource.type.nil?
        rtn
    end
end


For this you have to create a custom controller. Just an example implementation

#POST create
def create
  student = Student.create(params[:student]) # normal crud with whatever your form params
  sign_in(User.find(student.id)) # this actually signs in the user
  # now redirect the student to the dash board / whatever page manually
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号