On Wednesday, 29 August 2012 08:07:18 UTC-5, Ruby-Forum.com User wrote:
>
> My controller code is as follows, 
>
>
>   def index 
>   end 
>
>   def show 
>     if params[:cat_id] 
>     # cat_id is fetched from DB(a table with two columns id and name) 
>     #mapped as cat_id in routes file 
>       @articles = Article.find_all_by_name(params[:cat_id].gsub(/\-/, ' 
> ')) 
>       render :action => :index 
>     end 
>   end 
>
> How to do pagination using will_paginate on this. 
>
> Here @articles is an array which fetches articles based on category 
> names. The output of this is as follows, 
>
> An index page with 3 category names and relevant articles under each 
> category. Now i need to apply will_paginate under each category in index 
> page. How to do this. Adding ".paginate(:page => params[:page], 
> :per_page =>3) returns undefined method 'total pages' since its an 
> array. Could you please advise me as to how pagination can be done. 
> Further, the url structure is article/categoryname/articlename (formed 
> using slug). 
>


Use where instead of the find_by helpers.

`Article.where("name = ?", params[:cat_id].gsub(/\-/, '')).paginate({
  :page => params[:page], :per_page =>3 })`

find_all_by_* does it's job literally, it finds them all and it defeats the 
logic in it's name to add pagination to it.  So if you want pagination use 
where.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msg/rubyonrails-talk/-/nwdMDP2b1RUJ.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to