I have a bunch of different controllers, and I want to be able to do the standard \”Welcome, User.\” How do I assign the user variable to make it possible to access from any controller?
Here\’s what I have so far in the application controller:
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
Here\’s my application.html.haml file:
!!!
%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
What\’s the best approach?
Thanks!





Rating:
The post mvc – Easy Rails Issue: Global User Variable? appeared first on Javascript ASK.
via Javascript ASK http://javascriptask.phpfogapp.com/mvc-easy-rails-issue-global-user-variable.html
No comments:
Post a Comment