0

looooong time reader and knowledge attainer, first time poster. I've started learning Rails and I definitely need help.

I am following a tutorial with this code:

class BooksController < ApplicationController
  def index
    if params[:query].present?
      @books = Book.search(params[:query], page: params[:page])
    else
      @books = Book.all.page params[:page]
    end
  end

And I am trying to apply this to my own project (I have 'destinations' not 'books'). What I don't understand is what is the .page parameter and where is it generated from?

I am getting an "undefined method "page" on my Controller and I don't understand what it is.

This is my code:

class DestinationsController < ApplicationController
def index
    if params[:query].present?
      @destination = Destination.search(params[:query], page: params[:page])
    else
      @destination = Destination.all.page params[:page]
    end
  end

The only field I have in the Destinations table is country. I thought maybe that could replace it but alas, no. Yes, I'm very new to this. Any help is appreciated.

user2809852
  • 3
  • 1
  • 3
  • 1
    That is from ***pagination gems*** either `will_paginate` or `kaminari` – Pavan Jul 04 '15 at 14:08
  • You have to install either of the two for you to work. – Pavan Jul 04 '15 at 14:26
  • Thank you, you pointed me in the right direction but after many hours, there was a space in a link to a javascript file causing the main issue. However, thank you I now know about pagination parameters! :) – user2809852 Jul 04 '15 at 15:14

1 Answers1

1

How deep is your programming knowledge, especially Ruby one?

The tutorial is clearly using a pagination gem, I'll take kaminari because it's the one I use.

The gem monkey patches relation to include the method page and per, and as you can clearly see in Kaminari homepage, the method simply accepts page(page_number) and will take care to split your records using SQL LIMIT and OFFSET. The params[:page] will be a simple HTTP GET param ?page=12.

I notice you are also using Destination.search, that's not standard, did you create it? If yes, it should probably be something like Destination.search(params[:query]).page(params[:page]). If however it's using something like meta_search (which you shouldn't) or ransack, they are quite advanced and you should check for compatibility with Kaminari, which I'm quite sure it's present in Ransack

Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
  • Thanks @Fire-Dragon-DoL... I am a frontend developer and this is my first forray into the deep mysteries of the backend. As far as I can see, I have used: 'Destination.search(params[:query]).page(params[:page])' unless I am not understanding you. Thanks for the tips and yes I assumed it was pagination but didn't mention it. – user2809852 Jul 04 '15 at 18:08
  • From the code I'm seeing no, you didn't use `search.page`, in your question you clearly write `Book.search(params[:query], page: params[:page])` (page passed as param, not chained). To use it with Ransack, which I suppose you are using, check this answer: http://stackoverflow.com/a/17463115/312907 In the end, it's your choice if you want pagination for those records or not. If yes, you should add `gem 'kaminari'` to your Gemfile at least and fix the pagination for ransack as I suggested – Francesco Belladonna Jul 04 '15 at 19:58
  • "page passed as param, not chained". I see that now. I have a lot to learn. Thank you for the help. – user2809852 Jul 04 '15 at 20:48