Rails:使用Devise与单表继承 [英] Rails: Using Devise with single table inheritance

查看:124
本文介绍了Rails:使用Devise与单表继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用单表继承方式时,我遇到了一个问题:设计



我有两种不同类型的帐户组织如下:

 类帐户< ActiveRecord :: Base 
devise:database_authenticatable,:可注册
end

class User<帐户
end

class公司<帐户
结束

我有以下路线:

  devise_for:account,:user,:company 

用户注册在 / user / sign_up ,公司注册在 / company / sign_up 。所有用户使用单一表单在 / account / sign_in 帐户是父类)登录。 >

但是,通过此表单登录只是对帐户范围认证。随后的请求,例如 / user / edit / company / edit 将用户引导到登录屏幕相应的范围。



如何让Devise识别帐户的类型并对相关范围进行身份验证?



任何建议都非常感激。

解决方案

我刚刚遇到了确切的情况(类名更改)题。这是我的解决方案(Devise 2.2.3,Rails 3.2.13):config / routes.rb中的



  devise_for:accounts,:controllers => {:sessions => 'sessions'},:skip => :注册
devise_for:users,:companies,:skip => :会话

在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))除非resource.type。零?
rtn
end
end

注意:自从您的帐户类仍将是:注册视图中的默认链接/ devise / shared / _links.erb将尝试发出,但new_registration_path(Accounts)将不起作用(我们:在路由图中跳过它)并导致错误。您必须生成设计视图并手动删除它。



帽子提示 https://groups.google.com/forum/?fromgroups=#!topic/plataformatec-devise/s4Gg3BjhG0E ,指出我在正确的方向。


I am having a problem getting Devise to work the way I'd like with single table inheritance.

I have two different types of account organised as follows:

class Account < ActiveRecord::Base
  devise :database_authenticatable, :registerable
end

class User < Account
end

class Company < Account
end

I have the following routes:

devise_for :account, :user, :company

Users register at /user/sign_up and companies register at /company/sign_up. All users log in using a single form at /account/sign_in (Account is the parent class).

However, logging in via this form only seems to authenticate them for the Account scope. Subsequent requests to actions such as /user/edit or /company/edit direct the user to the login screen for the corresponding scope.

How can I get Devise to recognise the account 'type' and authenticate them for the relevant scope?

Any suggestions much appreciated.

解决方案

I just ran into the exact scenario (with class names changed) as outlined in the question. Here's my solution (Devise 2.2.3, Rails 3.2.13):

in config/routes.rb:

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

in 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

Note: since your Accounts class will still be :registerable the default links in views/devise/shared/_links.erb will try to be emitted, but new_registration_path(Accounts) won't work (we :skip it in the route drawing) and cause an error. You'll have to generate the devise views and manually remove it.

Hat-tip to https://groups.google.com/forum/?fromgroups=#!topic/plataformatec-devise/s4Gg3BjhG0E for pointing me in the right direction.

这篇关于Rails:使用Devise与单表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆