1 ruby ruby-on-rails devise cancan rolify
我对红宝石很新,我几个月来一直在苦苦挣扎.我广泛搜索并尝试了答案所说的但仍然没有运气.(我尝试了使用Ruby On Rails的多个用户模型,并设计了单独的注册路由,但是一条常用的登录路由但没有工作)
我目前有一个user.rb模型,它连接到设计和工作正常.
1-在注册页面上,我希望有3个按钮,这些按钮将导致单独的注册表单(业务,经理和现有用户各一个).我是否在routes.rb中进行了设置?2-表单将具有不同的属性,这些属性将填充其各自的数据库.3-表格填写完毕后,他们将被引导至各自的路线.用户到业务仪表板和经理到管理器仪表板的业务时的当前默认路由.这又是在routes.rb还是设计中?
我非常感谢任何指导!
我已经阅读了关于设计,康康和rolify的文档,但我似乎无法将它们全部集中在一起为我工作.
我对红宝石很新,我几个月来一直在苦苦挣扎.我广泛搜索并尝试了答案所说的但仍然没有运气.(我尝试了使用Ruby On Rails的多个用户模型,并设计了单独的注册路由,但是一条常用的登录路由但没有工作)
我目前有一个user.rb模型,它连接到设计和工作正常.
1-在注册页面上,我希望有3个按钮,这些按钮将导致单独的注册表单(业务,经理和现有用户各一个).我是否在routes.rb中进行了设置?2-表单将具有不同的属性,这些属性将填充其各自的数据库.3-表格填写完毕后,他们将被引导至各自的路线.用户到业务仪表板和经理到管理器仪表板的业务时的当前默认路由.这又是在routes.rb还是设计中?
我非常感谢任何指导!
我已经阅读了关于设计,康康和rolify的文档,但我似乎无法将它们全部集中在一起为我工作.
#user.rb
class User < ActiveRecord::Base
has_many :contibutions
rolify
# Include default devise modules. Others available are:
# :lockable, :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
def admin?
has_role?(:admin)
end
def self.find_for_oauth(auth, signed_in_resource = nil)
# Get the identity and user if they exist
identity = Identity.find_for_oauth(auth)
user = identity.user
if user.nil?
# Get the existing user from email if the OAuth provider gives us an email
user = User.where(:email => auth.info.email).first if auth.info.email
# Create the user if it is a new registration
if user.nil?
user = User.new(
name: auth.extra.raw_info.name,
#username: auth.info.nickname || auth.uid,
email: auth.info.email.blank? ? TEMP_EMAIL : auth.info.email,
password: Devise.friendly_token[0,20]
)
user.skip_confirmation!
user.save!
end
# Associate the identity with the user if not already
if identity.user != user
identity.user = user
identity.save!
end
end
user
end
end
Run Code Online (Sandbox Code Playgroud)
我会选择一个用户模型和一个两阶段注册.首先,他们会点击他们想要的按钮,每个按钮都会在URL中传递一个唯一的"角色"参数并转到设计注册页面.在这里,他们只输入他们的电子邮件/密码,我们会将URL中的参数传递给表单中的一个简单的"角色"隐藏字段.
然后作为步骤2,在技术上注册之后,将它们定向到单独的编辑帐户类型页面(每个用户具有不同的帐户,如下所述)以填写其余的细节.
型号:
车型/ user.rb
class User < ActiveRecord::Base
has_one :account
has_one :business_account
has_one :manager_account
end
Run Code Online (Sandbox Code Playgroud)
车型/ account.rb
class Account
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
车型/ business_account.rb
class BusinessAccount
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
车型/ manager_account.rb
class ManagerAccount
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
然后,使用devise,我将覆盖registrations_controller以在第一步简单注册表单中添加基于隐藏字段的角色(这只是电子邮件/密码/角色).
在该文件中,我还将覆盖after_signup_path方法,以重定向到我们在注册期间为其创建的相关帐户的edit_account类型页面.
首先是路线:
devise_for :users, :controllers => {:registrations => "registrations"}
resources :users do
resource :account
resource :business_account
resource :manager_account
end
Run Code Online (Sandbox Code Playgroud)
然后控制器(见代码中的注释):
控制器/ registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def create
build_resource(sign_up_params)
if resource.save
# you will name the following param. make sure it's in devise strong_params
# also the == will depend on how you pass the role - string, integer etc
if sign_up_params[:role] == "1"
user.add_role :standard
resource.build_account(user_id: resource.id) # code to create user account
elsif sign_up_params[:role] == "2"
user.add_role :manager
resource.build_manager_account(user_id: resource.id) # code to create user account
elsif sign_up_params[:role] == "2"
user.add_role :business
resource.build_business_account(user_id: resource.id) # code to create user account
end
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
respond_with resource
end
end
protected
# override the after signup path to your desired route, e.g
def after_sign_up_path_for(resource)
if sign_up_params[:role] == "1"
edit_user_account_path(resource.id)
elsif sign_up_params[:role] == "2"
edit_user_manager_account_path(resource.id)
elsif sign_up_params[:role] == "2"
edit_user_business_account_path(resource.id)
end
end
end
Run Code Online (Sandbox Code Playgroud)
以上将根据帐户类型将它们重定向到单独的帐户控制器/视图.这个解决方案可以为您节省很多麻烦.