views:

54

answers:

3

Hi

I have an object in ruby on rails for @user which contains username, password, etc

How can I ensure that the values are kept throughout all views?

Thanks

A: 

In your ApplicationController, add your object to the session and create a variable for it. Add a before_filter that calls the method that does that.

JRL
+1  A: 

I take it you want some sort of user sustem? logged in and tracking all over your system?

AuthenticatedSystem is something that can help you. there is a lot of documentation out their that will tell you exactly how to setup an environment that uses it. I personally use if for several systems I've made.

yopefonic
+7  A: 

If you set it up as follows:

class ApplicationController < ActionController::Base
  before_filter :set_user

protected 
  def set_user
    @user = User.find_by_id(session[:user_id]) 
  end
end

Then in all of controller, since they all inherits from ApplicationController, will have the @user value set.

Note: this will set the @user to nil if the session[:user_id] as not been set for this session.

For more on filters and the :before_filter, check this link out: Module:ActionController::Filters::ClassMethods

lillq