3

I have my mongoid query in the model

def self.get_result collection_name, hash_params, page, per_page 
  self.collection_name = collection_name
  @result_pg = self.where(hash_params).page(page).per(per_page)
end

I have it here because my collection names are passed as parameters and I don't have models for all my collection. Instead I have one model and I set its name dynamically based on the request parameter.

My controller code

Collection.get_result params[:state], hash_param, params[:page], params[:per]

My View code

<%= paginate @result_pg %>

When I use the above code I get

undefined method `current_page' for nil:NilClass   ERROR

1 Answer 1

4

I think the variable is being lost when you call it in the model. Instead, have it return the object back to you like so:

return self.where(hash_params).page(page).per(per_page) // put this in your model

Then, I'm the controller, connect the pieces:

@result_pg = Collection.get_result params[:state], hash_param, params[:page], params[:per] 
// in your controller like so

Right now, the controller isn't setting @result_pg, that is why it is nil and you are getting the nil error. Try this and let me know if that worked out for you.

Updated

Ok, as for the reason this happens. When Rails loads a controller, the variables that you declare with a @ symbol, like @bologna will be passed to the view and something will be done with them.

On the other hand, you are not technically declaring and instantiating that variable in the controller, you are doing it in the model, as per your source code that you posted above. Declaring the variable there can be done but it isn't useful because the controller has no idea that it even happened. When you tell the Class Collection to perform a method, usually you have that method return something back to you, if you don't, then the variable is lost.

It is similar to me asking someone to go to the store and buy me some groceries, you did the right things, giving the method all the information it needed, passing it the right variables to do its job correctly, but not telling it to actually come back with them, is where the error occurs. In your method, the groceries get purchased at the store and left there. The revised function I wrote for you tells it return with the groceries and put them in the variable @result_pg. The variable is declared in the Controller like it is supposed to be.

So in short, anything you want to have accessible in the view, needs to be declared in the controller. And anytime you want something back from a method, always have it return the information to you.

Also, it isn't entirely necessary to even have the method call to the Class Collection. In a project of my own where I use Kaminari, I just simply do the whole call from in the controller like so:

@notes = current_user.notes.page params[:page]
// My Application has :users that have_many :notes

So you could simplify it that way if you want, but the method that I suggested that fixes it the way you are doing it will work too, however you prefer.

I hope my explanation helped and wan't too long winded.

2
  • Thanks.. it worked, Do you have any idea why the variable is being lost. Jun 15, 2012 at 11:57
  • @AbhayKumar I updated my answer to help explain why my fix worked. I hope that it helps.
    – de_an777
    Jun 15, 2012 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.