Rails3-重定向登录用户



我正在为我的社区博客使用Devise和Can Can,目前正在寻找一种方法,在管理员和版主登录后直接将他们重定向到视图/admin/index页面。不确定我是否可以在routes.rb或登录表单中做到这一点?感谢任何帮助。。。

应用程序_控制器

class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
flash[:alert] = exception.message
redirect_to root_url
end
end

routes.rb文件

root :to => "articles#index"

sessions/new.html.erb

<h2>Sign In</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :password %><br />
<%= f.password_field :password %></p>
<% if devise_mapping.rememberable? -%>
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
<% end -%>
<p><%= f.submit "Sign In" %></p>
<% end %>

错误

NoMethodError in Devise/sessionsController#create - 
Application Trace | Framework Trace | Full Trace
app/controllers/application_controller.rb:11:in `after_sign_in_path_for'

application_controller中添加以下内容:

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if current_user.role?("admin") or current_user.role?("moderator")
      admins_index_path    # Make sure this route exists in your app!
    else
      stored_location_for(:user)
    end
  end
end

备选方案(适用于Devise 1.1.x):

class ApplicationController < ActionController::Base
  def stored_location_for(resource)
    if current_user && (current_user.role?("admin") || current_user.role?("moderator"))
      return admin_index_path    # Make sure this route exists in your app!
    end
    super(resource) 
  end
end

相关内容

  • 没有找到相关文章

最新更新