0

I have problem with kaminari gem. I have forum controller. On forum controller show action.View for show action is table with all topics associated with this forum. It looks like this:

 class ForumsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_forum, only: :show
  def index
    @forums = Forum.all
  end

  def show
   @forum_topics =  @forum.topics.page(params[:page])
  end

  private
  def set_forum
    @forum = Forum.find(params[:id])
  end
end

Forum show view:

= paginate @forum_topics

At the bottom of the page I see pagination links but when i click to take to the second page i have the same topics like in the first page? What's wrong? Any ideas?

Mateusz Urbański
  • 7,352
  • 15
  • 68
  • 133

1 Answers1

1

Seems that the pagination links don't use the same name parameter like the one your controller expects:

params[:page]

Make sure that the urls generated on the pagination links have something like page=2

Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35
  • my url is :http://localhost:3000/forums/1-asdas?page=2 so everything should work fine but it isn't... – Mateusz Urbański Oct 01 '13 at 12:36
  • In the url you posted there is a `1-asdas` which is not a valid id. In your set_forum method you call `find` but I guess it raises an ActiveRecord::RecordNotFound. – Lazarus Lazaridis Oct 01 '13 at 12:40
  • It not raises any error because i have to_param method in my forum model: def to_param [id, name.parameterize].join("-") end – Mateusz Urbański Oct 01 '13 at 12:44
  • Yes but have you changed the default find method in order search by param and not by id? The ActiveRecord find method searches by id by default. – Lazarus Lazaridis Oct 01 '13 at 12:47
  • when i deleted all to_params method from my app and restore the default it's still not working – Mateusz Urbański Oct 01 '13 at 12:54
  • Did You have any other ideas? – Mateusz Urbański Oct 01 '13 at 13:34
  • Can you debug inside your controller's show action to see if @forum variable is set? – Lazarus Lazaridis Oct 01 '13 at 13:41
  • How can I do that? I think that forum variable is set because if forum variable will be not set i couldn't go to the forum show action. This is my app on github: https://github.com/MatUrbanski/rails_forum_app Maybe when you see all code you will have any idea. – Mateusz Urbański Oct 01 '13 at 14:15
  • Ok, so, post which url redirects to you show page the first time and then which url is generated by kaminari. – Lazarus Lazaridis Oct 01 '13 at 14:32
  • when i visit forum show path i have link : http://localhost:3000/forums/1-asdas and i have table with all post associated with this forum. At the bottom i have the pagination links, when i clicked next i have the following url: http://localhost:3000/forums/1-asdas?page=2 – Mateusz Urbański Oct 01 '13 at 14:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38435/discussion-between-grotori-and-mateusz-urbanski) – Lazarus Lazaridis Oct 01 '13 at 14:47