Operators in ruby on rails not recognized
I wrote this function:
if params[:id] > @max
@page = @max
end
开发者_如何学GoHere @max
is an integer, and the value of params[:id]
isn't nil
.
It says there is a problem in the first line, and problem is:
undefined method `>' for nil:NilClass
It doesn't recognize >
as an operator. Why is it so?
Yes, params[:id]
is nil
. That's what that error means. Perhaps you wanted params['id']
instead? If you have access to the console for your running app, try p params, params[:id]
and make your request again to see what values there are, and the value of params[:id]
.
It doesn't recognize it as an operator on the NilClass
like it says. params[:id]
must be nil. Check your Rails logs for the list of parameters coming into the request. My guess is that the param is named differently than you think. Try if params[:id].present? && params[:id] > @max
or params[:id].to_i > @max
to work around the exception.
精彩评论