我有一堆不同的控制器,我希望能夠執行標準的「歡迎,用戶」。如何分配用戶變量以便從任何控制器訪問?Easy Rails問題:全局用戶變量?
這裏是我迄今爲止在應用控制器:
class ApplicationController < ActionController::Base
before_filter :authorize
protect_from_forgery
private
def current_user
User.find(session[:user_id])
end
protected
def authorize
unless User.find_by_id(session[:user_id])
redirect_to login_url, :notice => "Please Login"
end
end
end
這是我的application.html.haml文件:
!!!
%html
%head
%title Pears
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
%header
= link_to('Home', '/')
- if session[:user_id]
Welcome,
= current_user.firstname
= link_to('Logout', logout_path, method: :delete)
- else
= link_to('Login', login_path)
= link_to('Signup', signup_path)
= yield
什麼是最好的方法呢?
謝謝!
嘿感謝!我認爲這是我需要的。對於那些有同樣問題的人,我只是在會話控制器「create」方法session' [firstname] = user.first_name'中添加了一個新聲明。謝啦! – jake