如何在与rails的会话中设置多维数组

Seb*_*ien 6 ruby session ruby-on-rails

我想做这样的事情:

session[:continent][:filter] = params[:filter]
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我得到了这个错误:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=
Run Code Online (Sandbox Code Playgroud)

Oll*_*lly 13

您需要首先将会话[:continent]初始化为哈希.试试这个:

session[:continent] ||= {}
session[:continent][:filter] = params[:filter]
Run Code Online (Sandbox Code Playgroud)