在Ruby on Rails上设置Devise的不同用户模型和注册路径 [英] Setting up different User models and registration paths for Devise on Ruby on Rails

查看:524
本文介绍了在Ruby on Rails上设置Devise的不同用户模型和注册路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的红宝石,我已经真的在努力这个几个月。我广泛搜索,并尝试了答案,但仍然没有运气。 (我尝试过多个用户模型使用Ruby On Rails并设计具有单独的注册路由,但是有一个常见的登录路由但是没有工作)



我目前有一个user.rb模型,它是连接到设计和工作正常。



1-在注册页面上,我想要有3个按钮,将导致单独的注册表单(一个用于业务,经理和已经存在的用户)。我是否在routes.rb中设置?
2-表单将具有不同的属性,将填充其各自的数据库。
3-表格完成后,他们将被引导到他们各自的路线。用户到当前默认路由,同时业务到业务仪表板和管理器到管理器仪表板。这是再次在routes.rb还是设计?



我非常感谢任何指导!



阅读通过文档的设计,cancan和rolify,但我似乎不能把它一起为我工作。



我是新来的红宝石,我有几个月来一直真的很挣扎。我广泛搜索,并尝试了答案,但仍然没有运气。 (我尝试过多个用户模型使用Ruby On Rails并设计具有单独的注册路由,但是有一个常见的登录路由但是没有工作)



我目前有一个user.rb模型,它是连接到设计和工作正常。



1-在注册页面上,我想要有3个按钮,将导致单独的注册表单(一个用于业务,经理和已经存在的用户)。我是否在routes.rb中设置?
2-表单将具有不同的属性,将填充其各自的数据库。
3-表格完成后,他们将被引导到他们各自的路线。用户到当前默认路由,同时业务到业务仪表板和管理器到管理器仪表板。这是再次在routes.rb还是设计?



我非常感谢任何指导!



阅读通过文件制作,cancan和rolify,但我似乎不能把它一起为我工作。

  #user.rb 
class User< ActiveRecord :: Base
has_many:contibutions

rolify
#包含默认设计模块。其他可用的是:
#:lockable,:timeoutable
devise:database_authenticatable,:可注册,:可确认,
:可恢复,可记忆,可追溯,可验证,,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)

#获取身份和用户,如果他们存在
identity = Identity.find_for_oauth(auth)
user = identity.user
如果user.nil?

#如果OAuth提供商给我们发送电子邮件,从电子邮件获取现有的用户
user = User.where(:email => auth.info.email).first if auth.info .email

#创建用户,如果它是一个新的注册
如果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,
密码:Devise.friendly_token [0,20]

user.skip_confirmation!
user.save!
end

#将身份与用户相关联(如果尚未)
如果identity.user!= user
identity.user = user
identity.save !
end
end
user
end
end


解决方案

我会用一个用户模型和两个阶段的注册。首先,他们将点击他们所需的按钮,每个按钮在URL中传递一个独特的角色参数,并进入设计注册页面。在这里,他们只输入他们的电子邮件/密码,我们会将该参数从URL传递给一个简单的角色隐藏字段。



然后作为第2步,在技​​术注册之后,他们被定向到单独的编辑帐户类型页面(每个用户具有不同的帐户,如下所示),以填写其余的细节。



型号:



models / user.rb

  class User< ActiveRecord :: Base 
has_one:account
has_one:business_account
has_one:manager_account
end

models / account.rb

 类帐户
belongs_to:user

models / business_account.rb

 code> class BusinessAccount 
belongs_to:user

models / manager_account.rb



  class ManagerAccount 
belongs_to:user

然后,使用devise,我将覆盖registrations_controller在第一步简单注册表单(这只是电子邮件/密码/角色)中基于隐藏字段添加一个角色。



在该文件中,我还会覆盖after_signup_path方法,以重定向到我们为注册过程中为他们创建的相关帐户的edit_account类型页面。



首先路由:

  devise_for:users,:controllers => {:registrations => 注册} 

资源:用户做
资源:帐户
资源:business_account
资源:manager_account
结束

然后控制器(见代码中的注释):



controllers / registrations_controller。 rb

  class RegistrationsController< Devise :: RegistrationsController 

def create
build_resource(sign_up_params)

如果resource.save

#你将命名以下参数。确保它在设计strong_params
#也==将取决于你如何传递角色 - 字符串,整数等

如果sign_up_params [:role] ==1
user.add_role:standard
resource.build_account(user_id:resource.id)#代码创建用户帐户
elsif sign_up_params [:role] ==2
user.add_role:manager
resource.build_manager_account(user_id:resource.id)#代码创建用户帐户
elsif sign_up_params [:role] ==2
user.add_role:business
资源。 build_business_account(user_id:resource.id)#代码创建用户帐户
end

如果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}如果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

#覆盖所需路由的注册路径,例如
def after_sign_up_path_for(资源)
如果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

以上将重定向到单独的帐户控制器/视图取决于帐户类型。这个解决方案可以帮你节省很多麻烦。


I am very new to ruby and I have been really struggling with this for months. I searched extensively and tried what the answers said but still no luck. (I tried Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route but didnt work)

I currently have a user.rb model and it is connected to devise and works fine.

1- On the sign-up page, I would like to have 3 buttons that would lead to separate registration forms (one each for business, manager and the already existing user). Do I set this up in routes.rb? 2- The forms will have different attributes that will populate their respective databases. 3- After completion of the form they will be directed to their respective routes. User to the current default route while business to the business dashboard and manager to the manager dashboard. Is this again in routes.rb or devise?

I would greatly appreciate any guidance!

I've read through the documentations for devise, cancan and rolify but I can't seem to bring it all together to work for me.

I am very new to ruby and I have been really struggling with this for months. I searched extensively and tried what the answers said but still no luck. (I tried Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route but didnt work)

I currently have a user.rb model and it is connected to devise and works fine.

1- On the sign-up page, I would like to have 3 buttons that would lead to separate registration forms (one each for business, manager and the already existing user). Do I set this up in routes.rb? 2- The forms will have different attributes that will populate their respective databases. 3- After completion of the form they will be directed to their respective routes. User to the current default route while business to the business dashboard and manager to the manager dashboard. Is this again in routes.rb or devise?

I would greatly appreciate any guidance!

I've read through the documentations for devise, cancan and rolify but I can't seem to bring it all together to work for me.

#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

解决方案

I'd go with one User model and a two stage signup. First they would click on their desired button, each one passing a unique 'role' param in the URL and going to the devise signup page. Here they would enter only their email/password and we would pass the param from the URL to a simple 'role' hidden field in the form.

Then as step 2, after technically registering, they are directed to a separate edit account type page (each user having a different account, outlined below) to fill in the rest of their details.

The models:

models/user.rb

class User < ActiveRecord::Base
  has_one :account
  has_one :business_account
  has_one :manager_account
end

models/account.rb

class Account
  belongs_to :user

models/business_account.rb

class BusinessAccount
  belongs_to :user

models/manager_account.rb

class ManagerAccount
  belongs_to :user

Then, using devise, I'd override the registrations_controller to add a role based on a hidden field in the first step simple registration form (which would just be email/password/role).

In that file, I'd also override the after_signup_path method, to redirect to an edit_account type page for the relevant account we create for them during signup.

First the routes:

devise_for :users, :controllers => {:registrations => "registrations"}

resources :users do 
  resource :account
  resource :business_account
  resource :manager_account
end

Then the controller (see comments within code):

controllers/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

The above would redirect them to a separate accounts controller/view depending on the account type. This solution would save you a lot of headaches down the line.

这篇关于在Ruby on Rails上设置Devise的不同用户模型和注册路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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