2016-12-24 2 views
0

나는 Rails '~> 5.0.0', '>= 5.0.0.1'을 사용하고 있으며 다른 모델이 주최자과입니다. 스폰서Devise 4.2에 의해 관리됩니다.어떤 모델 (다양 함)이 Devise로 인증되지 않은 경우 특정 페이지로 리디렉션

는 내가 원하는 것은 : 사용자가 그들의 프로파일 유형을 클릭하고 있으므로 해당 로그인 양식으로 리디렉션 할 수 있도록 특정보기로 리디렉션,를 인증되지 않은 합니다.

사실 나는 두 모델을 필요할 때 포괄적으로 관리하기 위해 devise_group 기능을 사용하고 있습니다.

내 파일은 다음과 같습니다

application_ontroller.rb

class BrochuresController < ApplicationController 
    # Ensure an organizer is logged in before going ahead. 
    before_action :authenticate_member! 
end 

모든 것이 잘 작동하는 것 같았다

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 

    devise_group :member, contains: [:organizer, :sponsor] 

    private 

    def after_sign_in_path_for(resource) 
     case resource 
     when Organizer 
      dashboard_path 
     when Sponsor 
      brochures_path 
     end 
    end 
end 

brochures_controller.rb하지만 난 어떤 방법을 액세스 할 때 브로셔 관리자 주최자 로그인 양식 으로 항상 리디렉션되었습니다. 스폰서가 혼란 스러울 수 있으므로 불쾌합니다.

  • First Solution

는 I 내가 ApplicationController 추가하도록 사용자가 자신의 프로파일을 선택할 수있는 중성 페이지로 리디렉션하는 방법을 발견했다.

def authenticate_member! 
    if member_signed_in? 
     super 
    else 
     redirect_to root_path, notice: "Select your profile to login!" 
    end 
end 

그리고 주최자로 로그인 할 때 제대로 작동합니다. 콘솔에서

super: no superclass method `authenticate_member!' for #<BrochuresController:0x007fa760e387f8> Did you mean? authenticate_sponsor! 

Extracted source (around line #13): 
11 def authenticate_member! 
12  if member_signed_in? 
13   super 
14  else 
15   redirect_to root_path, notice: "Select your profile to login!" 
16  end 

:

NoMethodError (super: no superclass method `authenticate_member!' for #<BrochuresController:0x007fa760e387f8> 
Did you mean? authenticate_sponsor!): 

app/controllers/application_controller.rb:13:in `authenticate_member!' 
    Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout 
    Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb 
    Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.1ms) 
    Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb 
    Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms) 
    Rendering /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb 
    Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms) 
    Rendered /Users/alex/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (143.0ms) 
  • My Solution

내가 super가 참조 모르는하지만 스폰서로 로그인 할 때 나는이 오류가 발생했습니다

  • New error

그게 문제 였기 때문에 나는 단지 authenticate_member! 방법을 바꾼다. N 와 ApplicationController 이런 식으로은 :

def authenticate_member! 
    if !member_signed_in? 
     redirect_to root_path, notice: "Select your profile to login!" 
    end 
end 

이제 모든 (또는에 듯) 잘 작동합니다.

나는 또한 검색을 발견하고 잘 작동하는 것으로 나타났습니다 after_sign_in_path_for 방법이 있습니다.

  • What I want to know is:
  1. (authenticate_member 및 after_sign_in_path_for ) 내 솔루션 수행은 올바른 방법인가?
  2. 해결 방법이 더 있습니까?
  3. 컨트롤러 작업에서 사용자에게 권한을 활성화/비활성화 할 수 있습니까? - 일부는 주최자 용이고 일부는 주최자 용이고 일부는 주자 용이고 일부는 두 모델 용입니다..

BTW : 나는 충분한 정보와 문서에 대한 devise_group를 찾을 수 없습니다, 그래서 누군가가이 기능을 사용하여 더 나은 경험이있는 경우 당신이 그것에 대해 문서의 종류를 공유하고 싶습니다.

답변

0

컨트롤러는 모두 ApplicationController에서 상속되므로 쉽게 authenticate_member를 정의 할 수 있습니다! 각 컨트롤러에 대해 예 :

# organizer_controller.rb 
def authenticate_member! 
    if !member_signed_in? 
    redirect_to organizers_path, notice: "Organizer notice" 
    end 
end 

# sponsor_controller.rb 
def authenticate_member! 
    if !member_signed_in? 
    redirect_to sponsor_root_path, notice: "Notice for sponsors" 
    end 
end