2014-11-10 2 views
1

저는 루비에 대해 매우 익숙하며 몇 달 동안 정말로 고민하고 있습니다. 나는 광대하게 수색하고 대답이 말한 것을 시도했다. 그러나 아직도 운이 없다. (내가 시도한 Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route하지만 didnt 작업)Ruby on Deviles의 다른 사용자 모델과 등록 경로 설정하기

나는 현재 user.rb 모델을 가지고 있으며, 잘 작동하도록 잘 연결되어있다.

1- 가입 페이지에서 별도의 등록 양식 (비즈니스, 관리자 및 기존 사용자 용으로 하나씩)으로 연결되는 3 개의 버튼이 있습니다. 내가 routes.rb에서 이것을 설정합니까? 2- 폼에는 각각의 데이터베이스를 채울 다양한 특성이 있습니다. 3 양식 작성이 완료되면 해당 경로로 안내됩니다. 사용자가 비즈니스 대시 보드 및 관리자에게 관리자 대시 보드로가는 동안 현재 기본 경로로. 다시 route.rb에 있습니까? 아니면 궁리하고 있습니까?

나는 어떤 지침을 주셔서 감사합니다!

나는 devise, cancan 및 rolify에 대한 문서를 읽었지만 모두 함께 사용하면 효과가없는 것처럼 보입니다.

나는 루비에 새 것이므로 몇 달 동안 정말로 고민하고있다. 나는 광대하게 수색하고 대답이 말한 것을 시도했다. 그러나 아직도 운이 없다. (내가 시도한 Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route하지만 didnt 작업)

나는 현재 user.rb 모델을 가지고 있으며, 잘 작동하도록 잘 연결되어있다.

1- 가입 페이지에서 별도의 등록 양식 (비즈니스, 관리자 및 기존 사용자 용으로 하나씩)으로 연결되는 3 개의 버튼이 있습니다. 내가 routes.rb에서 이것을 설정합니까? 2- 폼에는 각각의 데이터베이스를 채울 다양한 특성이 있습니다. 3 양식 작성이 완료되면 해당 경로로 안내됩니다. 사용자가 비즈니스 대시 보드 및 관리자에게 관리자 대시 보드로가는 동안 현재 기본 경로로. 다시 route.rb에 있습니까? 아니면 궁리하고 있습니까?

나는 어떤 지침을 주셔서 감사합니다!

나는 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 
+0

당신이 설정 한 코드를 공유 할 필요가 있습니다.이 코드는 경험을 통해 말할 수 있지만, 움직이는 부분이 많이 있습니다. 내 의견으로는이 질문은 너무 광범위하다. – Anthony

+0

어떤 파일을 공유해야합니까? routes.rb? – angelraise

+0

개인적으로 나는 거대한 고통을 느끼는 여러 사용자 모델을 발견했습니다. 그 경험 후 나는 '쉬운 역할'보석을 사용하여 단일 모델을 사용했고 각 사용자 유형별로 별도의 계정 모델을 만들었습니다. easy_roles하지만 구식입니다. – RichardAE

답변

4

하나의 사용자 모델과 2 단계 가입으로 가고 싶습니다. 처음에는 URL에 고유 한 '역할'매개 변수를 전달하고 개발자 계정 가입 페이지로 이동하여 원하는 버튼을 클릭합니다. 여기서는 이메일/비밀번호 만 입력하면 URL의 매개 변수를 간단한 '역할'숨김 필드로 전달합니다.

기술적으로 등록한 후 2 단계로서 나머지 세부 사항을 채우기 위해 별도의 편집 계정 유형 페이지 (각 계정에 다른 계정을 가지고 있음)로 이동합니다.

모델 :

모델/user.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을 무시할 것, 고안 사용이 것 그냥 이메일/비밀 번호/역할).

해당 파일에서 가입시 생성 된 관련 계정의 edit_account 유형 페이지로 리디렉션하기 위해 after_signup_path 메소드를 재정의합니다.

첫 번째 경로 :

다음
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 

(가) 위의 별도의 계정으로 리디렉션 것 컨트롤러 /보기를 클릭하십시오. 이 솔루션을 사용하면 여러 가지 골치 거리를 줄일 수 있습니다.