English 中文(简体)
Can I use Struct.new as a Rails model? Or: How to create anonymous structured non-db backed sessions?
原标题:

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).

最佳回答

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

问题回答

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

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




相关问题
why the session in iis automatically log out?

I used iis6, and when i called a function Directory.delete(), the all the session relate this website will be logged out. and i debugged the website, i found no exception. any one have ideas on this ? ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Can I get the size of a Session object in bytes in c#?

Is it possible to get the size(in bytes) of a Session object after storing something such as a datatable inside it? I want to get the size of a particular Session object, such as Session["table1"], ...

提供严格分类的出席会议物体

提供严格分类的与会机会的最佳方式是什么? 我正计划转而选择矩阵,这正在促使汇编者抱怨我的幻觉方案拟订方法......

PHP Session is not destroying after user logout

I m trying to create an authentication mechanism for my PHP Application and I m having difficulty destroying the session. I ve tried unsetting the authentication token which was previously set within ...

热门标签