0

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 error occurred while evaluating nil.<<

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

4

2 に答える 2

0

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.

于 2010-12-08T06:32:17.873 に答える
0

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.

于 2010-12-08T06:32:28.787 に答える