kakubei
kakubei

Reputation: 5400

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.

Upvotes: 4

Views: 2897

Answers (2)

Greg Silcox
Greg Silcox

Reputation: 36

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

Upvotes: 2

kakubei
kakubei

Reputation: 5400

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 %>

Upvotes: 14

Related Questions