开发者

Rails param value returned as an empty int?

开发者 https://www.devze.com 2023-03-24 22:49 出处:网络
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.

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>
// @pa开发者_运维技巧ge.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).


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?

0

精彩评论

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