Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails can't dup NilClass error on params

I've got this in my controller:

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

And am getting the can't dup NilClass error.

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

It will still appear even if I do this:

page = params[:page]

Then below:

paginate(page)

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

Edit

I've discovered that substituting an integer for params[:page] results in a slightly different error: can't dup FixNum so maybe the issue is not with params but something else. Still don't know how to solve it though.

like image 247
kakubei Avatar asked Dec 04 '22 18:12

kakubei


2 Answers

My mistake. I apologize profusely for this, but I was using paginate inappropriately. I wish I could delete this question, but there doesn't seem to be a way. The proper way to use it is:

paginate(:page => params[:page]

I guess that's what happens when you've been staring at code for too long :)

Maybe it will help others deal with Paginate. I really like it, but the errors could really be a little more descriptive.

For example, I will always get an unknown method error unless I do this:

<%= will_paginate @artists if @artists.respond_to? :total_pages %>
like image 184
kakubei Avatar answered Dec 07 '22 08:12

kakubei


Thanks for not removing this question. I was being equally neglegent and changing: paginate( params[ :page ] ) to paginate( page: params[ :page ] ) fixed it.

like image 41
Greg Silcox Avatar answered Dec 07 '22 08:12

Greg Silcox