在RubyonRails上为Devise设置不同的用户模型和注册路径



我对ruby很陌生,几个月来我一直在为此而挣扎。我进行了广泛的搜索,并尝试了答案,但仍然没有成功。(我用Ruby On Rails尝试了多个用户模型,并设计了有单独的注册路由,但只有一个通用的登录路由,但不起作用)

我目前有一个user.rb模型,它可以连接设计并运行良好。

1-在注册页面上,我希望有3个按钮,它们将导致单独的注册表格(企业、经理和现有用户各一个)。我在routes.rb中设置了这个吗?2-表单将具有不同的属性,这些属性将填充各自的数据库。3-填写表格后,他们将被引导到各自的路线。用户到当前默认路由,业务到业务面板,经理到经理面板。这又是routes.rb还是design?

我将非常感谢任何指导!

我已经通读了设计、取消和复制的文件,但我似乎无法将其全部整合起来为我工作。

我对ruby很陌生,几个月来我一直在为此而挣扎。我进行了广泛的搜索,并尝试了答案,但仍然没有成功。(我用Ruby On Rails尝试了多个用户模型,并设计了有单独的注册路由,但只有一个通用的登录路由,但不起作用)

我目前有一个user.rb模型,它可以连接设计并运行良好。

1-在注册页面上,我希望有3个按钮,它们将导致单独的注册表格(企业、经理和现有用户各一个)。我在routes.rb中设置了这个吗?2-表单将具有不同的属性,这些属性将填充各自的数据库。3-填写表格后,他们将被引导到各自的路线。用户到当前默认路由,业务到业务面板,经理到经理面板。这又是routes.rb还是design?

我将非常感谢任何指导!

我已经通读了设计、取消和复制的文件,但我似乎无法将其全部整合起来为我工作。

#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

我会选择一个用户模型和两个阶段的注册。首先,他们会点击自己想要的按钮,每个按钮在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

class Account
  belongs_to :user

型号/business_account.rb

class BusinessAccount
  belongs_to :user

models/manager_account.rb

class ManagerAccount
  belongs_to :user

然后,使用design,我会覆盖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

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

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

以上内容将根据帐户类型将它们重定向到单独的帐户控制器/视图。这个解决方案将为您省去很多麻烦。

最新更新