分别验证用户和管理员

时间:2014-04-22 06:39:09

标签: ruby-on-rails ruby ruby-on-rails-4

class ApplicationController < ActionController::Base

   protect_from_forgery

   skip_before_filter :authenticate_user! , :only => ["welcome#index"]

   # before_filter :authenticate_user! :except => ["welocme#index"]

   def after_sign_in_path_for(user)
      # user_dashboard_index_path
      user_dashboard_index_path
   end

   def after_sign_out_path_for(user)
      welcome_index_path
   end

   after_filter :authenticate_admin!

   def after_sign_in_path_for(admin)
      admin_dashboard_index_path
   end

   def after_sign_out_path_for(admin)
      welcome_index_path
   end

end

管理员不应访问用户信息中心,同样用户也不应访问管理信息中心。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我在我的项目中完成了:

 protect_from_forgery with: :exception

def after_sign_in_path_for(resource)
if user_signed_in?
  user_dashboard_index_path
elsif admin_signed_in?
    admin_dashboard_index_path
else
   xyz_path
end
end

注销相同:

def after_sign_out_path_for(resource)
if user_signed_in?
   welcome_index_path
elsif admin_signed_in?
    welcome_index_path
else
    xyz_path
end
end

用于身份验证:

in(welcome / index)

<% if user_signed_in? %>
   contant_of user
<% else %>
   you are not authenticated #admin can not authenticate this page
 <% end %>

希望它会有所帮助