Initialize a session var as array in Rails 3 -


i need initialize session var array. that?

when try this:

session[:complains_id] << params[:id] 

i message

you have nil object when didn't expect it! might have expected instance of array. error occurred while evaluating nil.<<

i know have initialize var dont know where.

thanks!

just do:

  (session[:complains_id] ||= []) << params[:id] 

which saying:

  1. if session[:complains_id] returns nil, set [].

  2. append params[:id] session[:complains_id]

demo:

ruby-1.9.2-p136 :007 > (session[:complains_id] ||= []) << 5  => [5]  ruby-1.9.2-p136 :008 > (session[:complains_id] ||= []) << 3  => [5, 3]  ruby-1.9.2-p136 :009 > (session[:complains_id] ||= []) << 1  => [5, 3, 1]  

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -