2011-12-21 129 views
0

使用Devise并希望用户在注册后转到特定页面。这是踢球者;用户可以通过单选按钮注册为买方或工作人员。当他们注册为工人时,我希望他们去特定的路径。Ruby on Rails:在注册Worker角色后设计重定向

到目前为止,我现在的代码,买方和工人都将走到同一条路径。 '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

你应该在用户模型中有一个名为worker的方法,就像这个'def worker?角色=='工人'结束' – cristian 2011-12-21 10:10:32

+0

嘿@八达通保罗,是的。作为def worker,我在user.rb中拥有该方法?返回角色=='worker'end – 2011-12-21 10:13:02

+0

你测试过了吗?方法来查看它返回所需的结果? – cristian 2011-12-21 10:24:07

回答

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 

(还要确保您拥有最新版本的设计)