Forum: Ruby on Rails setting session data from a post

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
91c825f8b4f88257f3439d0e73cf1130?d=identicon&s=25 Bryan W. (railsroad)
on 2007-02-21 22:45
I'm using acts_as_authenticated, and I'm trying to run some tests where
the user must be registered. In the past, I've just included a user id
in the session data.

def test_login_loads_from_post_session
  post :new, nil, { :user => 7 }
  session = assigns(:session)
  assert_equal 7, session[:user]

My assert statement seems to be failing  because my session is not
showing me as being logged in.

Executing break point at
in `login_required'
irb(#<LinksController:0x2481fec>):001:0> session
=> #<ActionController::TestSession:0x24805fc @attributes={:user=>7,
"flash"=>{}}, saved_attributesnil, session_id""
irb(#<LinksController:0x2481fec>):002:0> session[:user]
=> nil

What I don't understand is why I can't access the attributes that are
listed in the session object. You can see that the @attributes hash has
the value 7 for the key :user.

What am I doing wrong?


def login_required
  username, passwd = get_auth_data
  self.current_user ||= User.authenticate(username, passwd) || :false if
username && passwd
  return true if logged_in? && authorized?
  respond_to do |accepts|
    accepts.html do
      session[:return_to] = request.request_uri
      redirect_to :controller => 'account', :action => 'login'
    accepts.xml do
      headers["Status"]           = "Unauthorized"
      headers["WWW-Authenticate"] = %(Basic realm="Web Password")
      render :text => "Could't authenticate you", :status => '401
91c825f8b4f88257f3439d0e73cf1130?d=identicon&s=25 Bryan W. (railsroad)
on 2007-02-22 04:09
This topic is locked and can not be replied to.