Rails 参数值返回为空 int?
这可能是一个愚蠢的问题,但我正在关注有关 Rails 的视频广播,现在我正在使用一个 2 个视图的控制器。我在参数方面遇到了一个小问题。
第一个视图有这样的内容:
<%= link_to("link", {:action => 'hello', :id => 1, :page => 11}) %>
我的控制器应该采用 params[:page] 然后将其解析为 int (.to_i),然后以实例变量的形式返回值,如下所示:
@page = params[:page].to_i
我的第二个视图有这样的内容:
correct output: <%= params['page'].to_i + 1 %> <br>
// this works alright, but I'm not using the controller
controller output: <%= @page + 1 %> <br>
// this throws an exception, 'nil value' | this works in the videocast
incorrect output: <%= @page.to_i + 1 %> <br>
// @page.to_i returns a value of 0, so it's 1 total
这很愚蠢,但我不想从左脚开始。
谢谢, Leo
PD:我正在使用 WEBrick、Ruby 192 和 Rails 3(在 Windows 上)。
This is probably a silly question, but I'm following a videocast about Rails, right now I'm working with a controller a 2 views. I'm having a minor issue with a parameter.
View one has something like this:
<%= link_to("link", {:action => 'hello', :id => 1, :page => 11}) %>
My controller is supposed to take params[:page] then parse it as an int (.to_i) then return the value in the form of an instance variable, like this:
@page = params[:page].to_i
My second view has something like this:
correct output: <%= params['page'].to_i + 1 %> <br>
// this works alright, but I'm not using the controller
controller output: <%= @page + 1 %> <br>
// this throws an exception, 'nil value' | this works in the videocast
incorrect output: <%= @page.to_i + 1 %> <br>
// @page.to_i returns a value of 0, so it's 1 total
It's something silly but I don't want to start off with the left foot on this.
Thanks,
Leo
PD: I'm using WEBrick, Ruby 192 and Rails 3 (on Windows).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有点奇怪的错误,因为它对我有用。您是否在主操作中设置@page?我可以看到发生错误的唯一方法是您正在调用一个正在渲染未设置 @page 的页面的操作。你可以粘贴你的家庭行动吗?
Kind of an odd error since it worked for me. Are you setting @page in your home action? Only way I could see that error happening is that you are calling an action that is rendering a page where @page is not being set. Can you paste your home action?