1

我对红宝石很陌生,几个月来我一直在为此苦苦挣扎。我进行了广泛的搜索并尝试了答案所说的内容,但仍然没有运气。(我尝试了使用 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
# 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
4

1 回答 1

5

我会选择一个用户模型和一个两阶段注册。首先,他们会点击他们想要的按钮,每个人都会在 URL 中传递一个唯一的“角色”参数并进入设计注册页面。在这里,他们将只输入他们的电子邮件/密码,我们会将参数从 URL 传递到表单中的一个简单的“角色”隐藏字段。

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

型号:

模型/用户.rb

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

模型/account.rb

class Account
  belongs_to :user

模型/business_account.rb

class BusinessAccount
  belongs_to :user

模型/manager_account.rb

class ManagerAccount
  belongs_to :user

然后,使用设计,我将覆盖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

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

控制器/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

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

于 2014-11-10T20:43:07.440 回答