1
我爲我的社區博客使用Devise和Can-Can,目前正在尋找一種方法,將'僅'管理員和版主直接重定向到views/admin/index頁面一旦他們登錄。不知道我是否可以在routes.rb或Sign In表單中執行此操作?感謝所有幫助...Rails 3 - 重定向用戶登錄
application_controller
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"
會議/ 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'
@Jits - 我將如何你的建議與現有app_controller整合代碼塊?已發佈代碼。 Thx – ubique 2011-05-31 14:12:22
我已經更新了我的答案。你應該可以在application_controller中添加這個新的方法聲明。 「引擎蓋下」發生的事情是它會「覆蓋」Devise gem定義的現有'after_sign_in_path_for',因此Devise會選擇並使用它。 – Jits 2011-05-31 14:24:20
@Jits - 謝謝,但這已經產生了一個錯誤,與def after_sign_in_path_for有關。已經張貼在上面? – ubique 2011-05-31 14:49:43