具有Devise的Rails多态用户模型 [英] Rails polymorphic user model with Devise

查看:105
本文介绍了具有Devise的Rails多态用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这个问题已经问了很多次,但我的问题还有一点进一步的。



当我的应用程序建模时,我有两种类型的用户与用户模型具有多态关联。例如:

  class User< ActiveRecord :: Base 
belongs_to:profileable,:polymorphic => true
end

class User_Type_1< ActiveRecord :: Base
has_one:user,:as => :profileable
end

class User_Type_2< ActiveRecord :: Base
has_one:user,:as => :profileable
end

我做这个而不是STI的原因是因为 User_Type_1 有4个字段, User_Type_2 有一个类似20个字段,我不希望用户表具有许多领域(是的24-ish字段不是很多,但我宁愿大约没有〜20个字段空闲)



我明白这是如何工作的,我的问题是我想要注册表单只能用于注册类型为 User_Type_1 的用户,但要用于两者的登录表单。 (我将有一个应用程序的管理员端,将创建 User_Type_2 的用户)



我知道我可以使用 after_sign_in_path_for(资源)覆盖 AppicationController 以某种方式重定向到网站的正确部分登录。 :

  def after_sign_in_path_for(resource)
case current_user.profileable_type
当user_type_1
当user_type_2
返回user_type_1_index_path
返回user_type_1_index_path
end
end

所以我想我的问题是如何使表单与Devise一起使用,只允许注册类型为 User_Type_1 ,然后在sign_up后签名? / p>

另外,如果我打错了这个方法,那么正确的方法是什么?

解决方案

我能够回答自己的问题,把它放在这里,以便也许它c帮助别人有同样的问题。



登录问题很简单,只需使用默认设计登录名和 after_sign_in_path_for in ApplicationController 如上所述



我意识到表单问题的答案是在这里输入: / p>

我刚刚为 User_Type_1 创建了一个正常表单,嵌套属性为用户
并将其发布到 UserType1Controller
然后保存两个对象并调用 sign_in_and_redirect 来自Devise的帮助者

  class UserType1Controller< ApplicationController 
...
def create
@user = User.new(params [:user])
@ user_type_1 = UserType1.new(params [:patron])
@ user.profileable = @ user_type_1
@ user_type_1.save
@ user.save
sign_in_and_redirect @user
end
...
end

然后从上面的 after_sign_in_path_for 方法将其发送到正确的地方,这一切都很好。


So I know this question has been ask a ton of times but my question goes a little bit further.

When modeling my application I have two types of users that have a polymorphic association to the user model. Such as:

class User < ActiveRecord::Base
    belongs_to :profileable, :polymorphic => true
end

class User_Type_1 < ActiveRecord::Base
    has_one :user, :as => :profileable
end

class User_Type_2 < ActiveRecord::Base
    has_one :user, :as => :profileable
end

The reason I did this, instead of an STI, is because User_Type_1 has something like 4 fields and User_Type_2 has something like 20 fields and I didn't want the user table to have so many fields (yes 24-ish fields is not a lot but I'd rather not have ~20 fields empty most of the time)

I understand how this works, my question is I want the sign up form to only be used to sign up users of type User_Type_1 but the sign in form to be used to both. (I will have an admin side of the application which will create users of User_Type_2)

I know I can use the after_sign_in_path_for(resource) override in AppicationController somehow to redirect to the right part of the site on sign in. Something like:

def after_sign_in_path_for(resource)
    case current_user.profileable_type
    when "user_type_1"
        return user_type_1_index_path
    when "user_type_2"
        return user_type_1_index_path
    end
end

So I guess my questions are how would I make the form to work with Devise and only allow signups of type User_Type_1 and then sign them in after sign_up?

Also, if I am going about this the wrong way, what is the right way?

解决方案

I was able to answer my own question and am putting it here so that maybe it can help someone else with the same problem.

The login problem was easy, just use the default devise login and the after_sign_in_path_for in ApplicationController as described above

I realized the answer to the form question as typing it out here:

I just created a normal form for the User_Type_1 with nested attributes for User and had it post to the UserType1Controller Then saved both objects and called the sign_in_and_redirect helper from Devise

class UserType1Controller < ApplicationController
    ...
    def create
        @user = User.new(params[:user])
        @user_type_1 = UserType1.new(params[:patron])
        @user.profileable = @user_type_1
        @user_type_1.save
        @user.save
        sign_in_and_redirect @user
    end
    ...
 end

Then the after_sign_in_path_for method from above sent it to the right place and it was all good.

这篇关于具有Devise的Rails多态用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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