Lilz
Lilz

Reputation: 4091

preserve object ruby on rails

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

Upvotes: 0

Views: 116

Answers (3)

lillq
lillq

Reputation: 15389

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

Upvotes: 7

yopefonic
yopefonic

Reputation: 532

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.

Upvotes: 1

JRL
JRL

Reputation: 78003

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.

Upvotes: 0

Related Questions