Rail Session Model

How can I process important session information as if it were a model?

I started doing this, and some of them work. But I'm not sure what I am missing? Does anyone know a good place to start or where can I find good examples?

In addition, I can use the model to set session variables, but how to get them from the model, and not always use session [: blah] ... How can I get them from the model?

class Cart
  attr_reader :items

  def initialize(session)
    @session = session
    @session[:cart] ||= []
    @items ||= session[:cart]
  end

  def add_rooms(new_rooms,startdate,days)
    #remove 0 values
    new_rooms.delete_if {|k, v| v == "0" }
    rooms = []
    new_rooms.each do |k, v|
      #rname = Room.find(k)
      #night = Available.find_by_room_id(k)
      rooms << {:room_id => k, :people => v, :no_days => days, :startdate => startdate} 
    end

    @session[:cart] = rooms
    #@items = rooms

  end

end
+3
source share
4 answers

There are a few errors that should be considered with this approach.

  • , , , . Rails 2.3 cookie, , .
  • , , , .
+2
+1

+1 Railscast .

;

  def set_order_id(i)
    @cart_session[:order_id] = i
  end
  def has_order_id?
    @cart_session[:order_id]
  end
  def order_id
    has_order_id?
  end
+1

, .

def blah
  @session[:blah]
end

, , , attr_reader. -

session_reader :blah
+1

Source: https://habr.com/ru/post/1710468/


All Articles