我有一堆不同的控制器,我希望能够执行标准的“欢迎,用户”。如何分配用户变量以便从任何控制器访问?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