开发者

Creating Session in Rails Helper

I have the following code in my application helper.

  route = ActionController::Routing::Routes.recognize_path(current_uri)
  controller = route[:controller]
  action = route[:action]
  session['route']<< [controller.to_s,action.to_s]

I get the following error You have a nil object when you didn't expect it! You might have expected an instance of Array. The er开发者_JAVA百科ror occurred while evaluating nil.<<

Some quick logging and I see that controller and action work just fine. Can you not create sessions in helpers?


If you want the session to hold an array of these arrays (a "stack of routes" if you will), then you first need to make sure that session[:route] is non-nil:

session[:route] ||= []
session[:route] << [controller.to_s, action.to_s]

Otherwise, simply assign it:

session[:route] = [controller.to_s, action.to_s]

Also, you should use symbols as hash keys, not strings.


You can but the way you are doing it is wrong. When this code executes for the first time it gets session['route'] as nil. You can do

session['route'] = [controller.to_s, action.to_s]

Although, what are you trying to do here? I suppose there is a better of achieving what you intend to do here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜