0

Devise를 사용하고 사용자가 가입 한 후 특정 페이지로 이동하기를 원합니다. 여기 키커가 있습니다. 사용자는 라디오 버튼을 통해 구매자 또는 작업자로 등록 할 수 있습니다. 그들이 노동자로서 가입 할 때, 나는 그들이 특정한 길로 가기를 원합니다.Ruby on Rails : 작업자 역할에 가입 한 후 Devise Redirect

지금까지 구매자와 근로자 모두 동일한 경로로 이동합니다. 'current_user.worker?' 코드는 내가 올바르게 작동하지 않는다고 생각합니다.

new.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= radio_button_tag 'user[role]', 'worker' %> 
    <%= radio_button_tag 'user[role]', 'buyer' %> 
    <%= f.text_field :username %> 
    <%= f.email_field :email %> 
    <%= f.password_field :password %> 
    <%= f.password_field :password_confirmation %> 
    <%= f.submit 'Create Account', :class => 'button' %> 
<% end %> 

application_controller.rb

class ApplicationController < ActionController::Base 


    def after_sign_up_path_for(resource_or_scope) 
    if current_user.worker? 
     account_setup_path 
    else 
    end 
    end 

end 
+0

당신은 'def worker'와 같은 사용자 모델에서 worker라는 메소드를 가져야합니다; 역할 == 'worker'end' – cristian

+0

안녕하세요 @ Octopus-Paul, 네. def worker로 user.rb에이 메소드가 있습니까? return role == 'worker'end –

+0

당신은 작업자를 테스트 했습니까? 그것을보고 원하는 결과를 반환하는 방법? – cristian

답변

1

이 당신의 user.rb에 다음을 추가합니다 .. 당신이라는 사용자 테이블의 열을 가정 "역할"이고 그것은 "근로자"또는 "구매자"중 하나입니다.

def worker? 
    (self.role == "worker") 
end 

def buyer? 
    (self.role == "buyer") 
end 

그런 다음이 당신의 가입 코드와 동일한 컨트롤러에 넣어 ... 가입 후 작동합니다 (대부분의 경우 RegistrationController?)를

def after_sign_up_path_for(resource_or_scope) 
     account_setup_path if current_user.worker? 
     some_other_path if current_user.buyer? 

     # And if they are not a buyer or worker .. well. Redirect to root. 
     root_path 
    end 

(또한이 고안의 최신 버전이 있는지 확인)