Paginate without a gem Next, Previous, buttons for Name.order(:id).limit(10).offset(0)

前端 未结 2 433
青春惊慌失措
青春惊慌失措 2021-01-21 12:57

I\'ve been searching for a solution to my problem for over a week now. I have an assignment that I lost 10 pts on due to no next/prev functionality and ran out of time. I still

2条回答
  •  青春惊慌失措
    2021-01-21 13:32

    A few things here. Firstly, controller methods should be used with matching routes. It's a request cycle where you click a button on your app, it makes a request to your server, then your server response with information.

    Your next_button method is not going to work when you put it as a helper_method this way. In order to make your controller work, you should have routes that match to your controller method. Do a rake routes in your command line. You need to see something like this in your route.rb file

    get 'ripple/next', 'ripple#next'
    

    More about routes: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

    And in your controller, you could have

    def next
      page = params[:page]
      params[:page] += 1
      @ripples = Ripple.find_ripples_on_page(page, PAGE_SIZE)
      render :index
    end
    

    Then in your erb view, your should be 'visiting' this particular route, not calling the method.

    Secondly, you can not rely on class instance variable in your model. Instead, you should put it in your session or as part of a query string. As above controller code, I put it in session, which is a local storage. More about session: http://guides.rubyonrails.org/action_controller_overview.html#session

    Finally, your model.

    def find_ripples_on_page(page, PAGE_SIZE)
      Ripple.where( ... ) # you can figure this out, depending on if you want to rank by updated_at or other filter
    end
    

    Side note: helper_method in controller is for retrieving information not giving command to the controller. All this macro does is use class_eval and 'copy' the method you have in controller into the renderer.

    If you can use a gem, use will_paginate.

    http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

提交回复
热议问题