0
需要相同的代碼,我在我的網頁以下登錄檢查:Rails的DRY問題:在控制器和視圖
class LoungeController < ApplicationController
before_filter :confirm_logged_in
def index
end
end
而confirm_logged_in
定義在這裏:
class ApplicationController < ActionController::Base
protect_from_forgery
protected
def confirm_logged_in
return true if current_user
redirect_to(:controller => 'access', :action => 'login')
return false # halts the before_filter
end
def current_user
return false unless session[:user_id]
user = User.find(session[:user_id])
return false unless user
(user.display_name == session[:user_display_name]) ? user : nil
end
end
現在,我想用confirm_logged_in
也在app/views/layouts/application.html.erb
:
<% if confirm_logged_in %>
<div id="logged_in_as">You are logged in as <%= session[:user_display_name] %></div>
<div id="logout"><%= link_to("Logout", {:controller => "access", :action => "logout"}, :id => "logout_link") %></div>
<% end %>
你會如何表示st解決這個問題?我應該在哪裏定義confirm_logged_in
?
完美解決方案:)謝謝! – 2011-04-23 07:55:36