views:

259

answers:

3

Given the following example:

class AnonymousSession << Struct.new(:location, :preferences)
  def valid?
    ...
  end
  def new_record?
    ...
  end
end

While this interface is sufficient to create resourceful form and so on, it fails as soon as I want to save my form data to the session:

if session[:user] = AnonymousSession.create(params[:anonymous_session])
  #--> fails with "unknown key(s): location..."
  ...
end

The error message is about "unknown keys". Any clue how to make it work? I just need anonymous sessions without database backend. They are completely disposable due to their short live nature.

Maybe my approach is wrong anyway and there's already an elegant solution to using anonymous sessions? I had a look at AuthLogic but any example I found always comes with an ActiveRecord model (and thus bound to a database).

+1  A: 

I have used this solution, where my model derives from Tableless. I think it will work in your case as well.

nathanvda
This sounds like the way to go.
hurikhan77
A: 

You'd have to explain more as to what you're trying to accomplish. Why couldn't you just create an AnonymousSession class in /app/models?

class AnonymousSession

  attr_accessor :location, :preferences

  def new_record?
   # ...
  end

  def valid?
   # ...
  end

end
Nicholas C
The "Tableless" solution works perfectly for me because it also gives validations for free. A simple class would require too much work so this solution is no longer an option. But thanks anyway.
hurikhan77
+1  A: 

Ryan Bates has a couple Railscast episodes that might help you out: Session Based Model and Super Simple Authentication.

Andy Atkinson
That sounds interesting but I still think the Tableless suggestion fits my problem better. The user session consists of a street and house number which is validated by comparing it with a streets table and a street sections table and a geokit lookup. This connects the user to a districts table which holds data about next waste collection dates and so on.
hurikhan77