SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    mouse monkey
    Join Date
    Dec 1999
    Location
    UK
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] Make object available to every view

    I use a technique similar to the one described at http://www.aidanf.net/rails_user_aut...ation_tutorial for authentication in a rails application.

    I have the following declared in my Application controller

    Code:
      def current_user
        session[:user]
      end
    Which makes the currently logged in user's object available in all the controllers.

    Code:
    def edit
      @item=Blog.find(:first, :conditions=>["user_id=? and id=?", current_user.id, params[:id]])
    end
    In all of the pages behind a login, I need the user object available because the layout depends on the permissions assigned to the user. Currently I use the following code in every controller to make this available in the view.

    Code:
    @current_user = current_user
    Is there a better way to achieve this? So the current_user object is available automatically to every view without having to explicitly set it.

  2. #2
    mouse monkey
    Join Date
    Dec 1999
    Location
    UK
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The solution I've used is to overwrite the render method in the Application controller.

    Code:
    	def render(options = nil, deprecated_status = nil, &block)
    			@current_user = current_user
    		super	
    	end

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a before_filter in your application controller.

  4. #4
    mouse monkey
    Join Date
    Dec 1999
    Location
    UK
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Luke!

    I use before_filters throughout my app, don't know why I didn't think to include one in the application controller. Both methods work but the before_filter seems much neater. Thanks again.

  5. #5
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another way is to expose current_user as a helper method as well like so:

    Code:
      helper_method :current_user # allow views to access method
      def current_user
        @current_user ||= (session[:user] && User.find(session[:user])) || nil
      end
    
      def current_user=(user_id)
        session[:user] = user_id
      end
    Also, I suggest you store the user id instead of the user model in the session (like in my code example) to avoid some very confusing things from happening when the user model instance is edited.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •