If you use Mephisto as your blog engine, you should have known that Mephisto is not equipped with articles pagination. As a result, you have to figure out how to set up the pagination on your list of articles. There is a myth that it is quite difficult to set up pagination for your blog articles in Mephisto. In fact, the difficulty with Mephisto is that instead of using normal Ruby on Rails, Mephisto is using liquid template to render the articles which is quite new and complex to understand in order to customize up to your expectation.

In this article, we will go through how to set up articles pagination to work with the latest Mephisto blog engine - version 0.8.2

Essential Plugins installation

If you try to google around for this kind of topic, you will end up in artweb-design blog - http://www.artweb-design.de/projects/mephisto-plugin-paged-article-lists. The author of this article has quite a good plugin for Pagination in Mephisto that we can use. Unluckily, this article is quite outdated. I guess it is since 2007. Nevertheless, we still can use those plugins but with some modifications

You need to install 2 plugins mephisto_paged_article_list and will_paginate_liquidized using the following commands

script/plugin install http://svn.artweb-design.de/stuff/mephisto/mephisto_paged_article_list
script/plugin install http://svn.artweb-design.de/stuff/rails/will_paginate_liquidized

Fix the Incompatibility!

If right after finish installing the plugins and you go into your liquid template design page to add in the following line

{{ articles | prev_next_page_links: path_info }}

You will end up having the below error

undefined method 'page_count' for #

So what are happening here. Well, the latest Mephisto engine is equipped with will_paginate plugin 2.2 but it seems the plugin still assumes older version of will_paginate plugin is used. That's why there is a mismatch in the attribute call. Hence, we need to edit the plugin in order to make it compatible

Try to open file

RAILS_HOME/vendor/plugins/will_paginate_liquidized/lib/will_paginate/liquidized.rb

And change the allow array in function method_missing as following

# allow = [:current_page, :per_page, :total_entries, :page_count, :offset, 
#       :previous_page, :next_page, :empty?, :length, :sort_by]
allow = [:current_page, :per_page, :total_entries, :total_pages, :offset, 
       :previous_page, :next_page, :empty?, :length, :sort_by]

As you may notice, we have changed the page_count key to total_pages key

We need to change this key in another place

RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/link_helpers.rb

Modify the function link_to_next_page's first line as following

# return if pager.current_page == pager.page_count 
return if pager.current_page == pager.total_pages

Now if you restart the server, and refresh the blog home page, you will hit another error

undefined method 'path_for' for #

Well, it turns out that path_for is a function used in

RAILS_HOME/vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/link_helpers.rb

But it seems that this function does not come out from anywhere. If you analyze the source code, you will discover that, this path_for function is just try to grab the path_info argument and attach to the page_number in order to form a link. It ends up I decided to write the path_for function inside the same file so that the error will go away

def path_for( path_info, page_number )
    return path_info[:path] + "/page/" + page_number.to_s
end

Now, if you restart your server, and refresh the page, you should see what you expect initially. The pagination for your articles will work as it is supposed to

Conclusion

I hope this article can help some souls out there who are trying to solve this problem. I also have to spend quite a lot of times trying to figure out what was going on at that time. For those, who have better idea how to fix this kind of issue, I will welcome all the feedbacks.

5 Responses to “Setting up Mephisto Articles Pagination”

  1. yortz Says:
    Well man, first of all lemme thank you since you solved my problems with pagination, though i still do not have exactly clear few things: 1) when i svn co the plugin it downloads the whole thing with tags and trunk, actually i am using the trunk version tailored to my needs thanks to your wonderful blog post, but i am not sure the exact version you are running with mephisto 0.8.2 (the same one i am using) 2)do not know why, but when i browse via pagination, testing it in development it still complains about the following error (so i do not know if the trick needs further improvement), anyway this is what i got: WillPaginate::InvalidPage in MephistoController#dispatch "articles" given as value, which translates to '0' as page number vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:51:in `initialize' vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:86:in `new' vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:86:in `create' vendor/gems/will_paginate-2.2.2/lib/will_paginate/finder.rb:76:in `paginate' vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/mephisto_controller.rb:23:in `dispatch_list' app/controllers/mephisto_controller.rb:11:in `send' app/controllers/application.rb:108:in `get_requests_are_readonly' config/initializers/readonly_records.rb:18:in `with_readonly_records' app/controllers/application.rb:107:in `get_requests_are_readonly' also tried to change the snippet to mimic the digg navigation kinda thing, but it this time complains about another liquid error: so when using {{ articles Well man, first of all let me thank you since you solved my problems with pagination, though i still do not have exactly clear few things: 1) when i svn co the plugin it downloads the whole thing with tags and trunk, actually i am using the trunk version tailored to my needs thanks to your wonderful blog post, but i am not sure the exact version you are running with mephisto 0.8.2 (the same one i am using) 2)do not know why, but when i browse via pagination, testing it in development it still complains about the following error (so i do not know if the trick needs further improvement), anyway this is what i got: WillPaginate::InvalidPage in MephistoController#dispatch "articles" given as value, which translates to '0' as page number vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:51:in `initialize' vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:86:in `new' vendor/gems/will_paginate-2.2.2/lib/will_paginate/collection.rb:86:in `create' vendor/gems/will_paginate-2.2.2/lib/will_paginate/finder.rb:76:in `paginate' vendor/plugins/mephisto_paged_article_list/lib/paged_article_list/mephisto_controller.rb:23:in `dispatch_list' app/controllers/mephisto_controller.rb:11:in `send' app/controllers/application.rb:108:in `get_requests_are_readonly' config/initializers/readonly_records.rb:18:in `with_readonly_records' app/controllers/application.rb:107:in `get_requests_are_readonly' also tried to change the snippet to mimic the digg navigation kinda thing, but this time it complains about another liquid error: so when using {{ articles | will_paginate: path_info }} as specified in http://www.artweb-design.de/projects/mephisto-plugin-paged-article-lists i got Liquid error: undefined method `total_pages' for # anyway, thanks once again for your precious work!| will_paginate: path_info }} as specified in http://www.artweb-design.de/projects/mephisto-plugin-paged-article-lists i got Liquid error: undefined method `total_pages' for # anyway, thanks for your precious work, i'll try to investigate myself on this behaviour.
  2. yortz Says:
    Sorry for being such an ass... my fault! Your fix is working at least on a clean 0.8.2 mephisto install, now need to check what's breaking things in my other mephisto instance (plugins, monkey patching maybe.... who knows?) anyway thanks for this post, IMHO this should be put in the wiki! :)
  3. Scott Says:
    Hello, Thanks for posting this howto. I'm gotten it very close to working but am having a path error just like the one described here: http://groups.google.com/group/mephistoblog/browse_thread/thread/87f7af120e33cddf# The path get repeated in the url. If you have any ideas I'd be greatly appreciative. Thanks! s.
  4. Scott Says:
    Sorry about the quick second post but I didn't describe my trouble very well. After looking at things again I should have said that your pagination works but it doesn't work for sections. I get urls like http://asdf.com/secionname/sectionname/page/2 http://asdf.com/sectionname/page/sectionname/page/4 I imagine there's a pretty simple fix somewhere in link_helpers.rb where you're building the links but I'm not enough of a coder to figure it out. Anyway, thanks again. s.
  5. yortz Says:
    this is how I am paginating through different blog sections, it works on mephisto 0.8.2 I couldn't find any other quick and valuable solution to the :path issue. http://gist.github.com/177100

Sorry, comments are closed for this article.