Oded Harth
Oded Harth

Reputation: 4406

Operators in ruby on rails not recognized

I wrote this function:

  if params[:id] > @max
   @page = @max
    end

Here @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?

Upvotes: 0

Views: 345

Answers (2)

jxpx777
jxpx777

Reputation: 3641

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.

Upvotes: 0

Phrogz
Phrogz

Reputation: 303361

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].

Upvotes: 3

Related Questions