在 Rails Helper 中创建会话
我的应用程序助手中有以下代码。
route = ActionController::Routing::Routes.recognize_path(current_uri)
controller = route[:controller]
action = route[:action]
session['route']<< [controller.to_s,action.to_s]
我收到以下错误 当你没有预料到的时候,你得到了一个 nil 对象! 您可能期望一个 Array 的实例。 计算 nil 时发生错误。<<
一些快速记录,我发现控制器和操作工作得很好。不能在助手中创建会话吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望会话保存这些数组的数组(如果您愿意的话,可以是“路由堆栈”),那么您首先需要确保
session[:route]
为非零:否则,只需分配它即可:
此外,您应该使用符号作为哈希键,而不是字符串。
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:Otherwise, simply assign it:
Also, you should use symbols as hash keys, not strings.
你可以,但你做事的方式是错误的。当此代码第一次执行时,它会将 session['route'] 设为 nil。你可以做
虽然,你想在这里做什么?我想这里有更好的方法来实现你想要做的事情。
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
Although, what are you trying to do here? I suppose there is a better of achieving what you intend to do here.