简体   繁体   中英

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?

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 if params[:id].present? && params[:id] > @max or params[:id].to_i > @max to work around the exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM