1

私はルビーに非常に慣れていないので、何ヶ月もこれに苦労しています。私は広範囲に検索し、答えが言ったことを試しましたが、まだ運がありません. (Ruby On Rails で複数のユーザー モデルを試し、登録ルートを分けて、共通のログイン ルートを 1 つ持つように工夫しましたが、うまくいきませんでした)

私は現在 user.rb モデルを持っており、devise に接続されており、正常に動作しています。

1-サインアップページに、個別の登録フォームにつながる3つのボタンが必要です(ビジネス、マネージャー、および既存のユーザーにそれぞれ1つずつ)。これをroutes.rbで設定しますか? 2-フォームには、それぞれのデータベースに入力されるさまざまな属性があります。3-フォームの記入が完了すると、それぞれのルートに誘導されます。ユーザーは現在のデフォルト ルートに、ビジネスはビジネス ダッシュボードに、マネージャはマネージャ ダッシュボードに移動します。これはまたroutes.rbまたはdeviseにありますか?

ご指導いただければ幸いです。

私はdevise、cancan、およびrolifyのドキュメントを読みましたが、すべてをまとめて機能させることはできないようです。

私はルビーに非常に慣れていないので、何ヶ月もこれに苦労しています。私は広範囲に検索し、答えが言ったことを試しましたが、まだ運がありません. (Ruby On Rails で複数のユーザー モデルを試し、登録ルートを分けて、共通のログイン ルートを 1 つ持つように工夫しましたが、うまくいきませんでした)

私は現在 user.rb モデルを持っており、devise に接続されており、正常に動作しています。

1-サインアップページに、個別の登録フォームにつながる3つのボタンが必要です(ビジネス、マネージャー、および既存のユーザーにそれぞれ1つずつ)。これをroutes.rbで設定しますか? 2-フォームには、それぞれのデータベースに入力されるさまざまな属性があります。3-フォームの記入が完了すると、それぞれのルートに誘導されます。ユーザーは現在のデフォルト ルートに、ビジネスはビジネス ダッシュボードに、マネージャはマネージャ ダッシュボードに移動します。これはまたroutes.rbまたはdeviseにありますか?

ご指導いただければ幸いです。

私は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
4

1 に答える 1

5

1 つのユーザー モデルと 2 段階のサインアップを使用します。まず、ユーザーは目的のボタンをクリックし、それぞれが 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

models/business_account.rb

class BusinessAccount
  belongs_to :user

モデル/マネージャー_アカウント.rb

class ManagerAccount
  belongs_to :user

次に、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

次に、コントローラー (コード内のコメントを参照):

コントローラー/登録_コントローラー.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 に答える