简体   繁体   English

为Ruby on Rails上的Devise设置不同的用户模型和注册路径

[英]Setting up different User models and registration paths for Devise on Ruby on Rails

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) (我在Ruby On Rails上尝试了多个用户模型,并设计了单独的注册路径,但是有一个通用的登录路径,但是没有用)

I currently have a user.rb model and it is connected to devise and works fine. 我目前有一个user.rb模型,它可以连接设计并可以正常工作。

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). 1-在注册页面上,我想有3个按钮,这些按钮将导致产生单独的注册表格(每个用于企业,经理和已经存在的用户)。 Do I set this up in routes.rb? 我是否要在routes.rb中进行设置? 2- The forms will have different attributes that will populate their respective databases. 2-表单将具有不同的属性,这些属性将填充各自的数据库。 3- After completion of the form they will be directed to their respective routes. 3-完成表格后,他们将被定向到各自的路线。 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? 是在routes.rb中还是设计出来?

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. 我已经阅读了有关devise,cancan和rolify的文档,但似乎无法将所有内容结合起来为我工作。

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) (我在Ruby On Rails上尝试了多个用户模型,并设计了单独的注册路径,但是有一个通用的登录路径,但是没有用)

I currently have a user.rb model and it is connected to devise and works fine. 我目前有一个user.rb模型,它可以连接设计并可以正常工作。

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). 1-在注册页面上,我想有3个按钮,这些按钮将导致产生单独的注册表格(每个用于企业,经理和已经存在的用户)。 Do I set this up in routes.rb? 我是否要在routes.rb中进行设置? 2- The forms will have different attributes that will populate their respective databases. 2-表单将具有不同的属性,这些属性将填充各自的数据库。 3- After completion of the form they will be directed to their respective routes. 3-完成表格后,他们将被定向到各自的路线。 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? 是在routes.rb中还是设计出来?

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. 我已经阅读了有关devise,cancan和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

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. 首先,他们将单击所需的按钮,每个按钮都会在URL中传递一个唯一的“角色”参数,然后转到设备注册页面。 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. 在这里,他们只输入电子邮件/密码,我们会将参数从URL传递到表单中的简单“角色”隐藏字段。

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. 然后,在步骤2中,经过技术注册后,他们将被定向到一个单独的编辑帐户类型页面(每个用户都有一个不同的帐户,如下所述),以填写其其余详细信息。

The models: 型号:

models/user.rb 型号/user.rb

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

models/account.rb 型号/帐户.rb

class Account
  belongs_to :user

models/business_account.rb 型号/business_account.rb

class BusinessAccount
  belongs_to :user

models/manager_account.rb 型号/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). 然后,使用devise,我将覆盖registrations_controller以在第一步简单注册表单(这只是电子邮件/密码/角色)中基于隐藏字段添加角色。

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. 在该文件中,我还将覆盖after_signup_path方法,以重定向到我们在注册期间为他们创建的相关帐户的edit_account类型页面。

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 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. 此解决方案将为您省去很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Ruby On Rails 的多个用户模型,并设计为具有单独的注册但一个共同的登录 - Multiple user models with Ruby On Rails and devise to have separate registration but one common login 使用 Ruby On Rails 的多个用户模型,并设计为具有单独的注册路径但有一个通用的登录路径 - Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route 用Devise跟踪多个注册路径 - Rails multiple registration paths with Devise Ruby on Rails - 设计注册链接无效 - 未定义方法`user_registration_path&#39; - Ruby on Rails - Devise sign up link not working - undefined method `user_registration_path' Rails:如何让设计中的两个用户模型通过注册 - Rails: how to let two user models in devise go through registration rails devise-如何为两个不同的用户模型进行2个devise认证? - rails devise - how to have 2 devise authentications for two different user models? Ruby on rails使用rest API调用和Devise进行用户注册 - Ruby on rails User registration using rest API call and Devise 在Rails 4中如何测试设计自定义注册路径? - In Rails 4 How to Test Devise Custom Registration Paths? 使用Rails中的ruby进行管理员和用户模型的单一登录 - Single sign in for admin and user models using devise in ruby on rails Rails + Devise + API +用户注册 - Rails + Devise + API + User Registration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM