Rails cannot duplicate NilClass error in parameters

I have this in the controller:

@artists = Artist.where("artist LIKE ?", "%#{params[:term]}%").limit(500).paginate(params[:page]) 

And I get can't dup NilClass .

Now params [: term] and params [: page] are two different variables, so why should the error appear? If I remove the second params variable, the error will disappear.

It will still appear, even if I do the following:

 page = params[:page] 

Then below:

 paginate(page) 

I would like to understand why this is happening and how to fix it, how to use 2 variables from params () on the same line without this error.

Edit

I found that integer substitution for params [: page] leads to a slightly different error: can't dup FixNum , so maybe the problem is not with the parameters, but with something else. However, I do not know how to solve this.

+4
source share
2 answers

My mistake. I apologize for this, but I misused paginate. I wish I could remove this question, but it doesn't seem to be that way. The correct way to use:

 paginate(:page => params[:page] 

I assume that what happens when you looked at the code for too long :)

Perhaps this will help others sort out Paginate. I really like it, but the errors can be a little more visual.

For example, I always get an unknown method error if I do not:

 <%= will_paginate @artists if @artists.respond_to? :total_pages %> 
+14
source

Thank you for not removing this question. I was just as sloppy and changed: paginate (params [: page]) in paginate (page: params [: page]) fixed it.

+2
source

Source: https://habr.com/ru/post/1392659/


All Articles