I have a collection of people that is paginated with will_paginate
#people = Person.paginate :page => params[:page],
:limit => 10,
:conditions => ['company_id = ? ' , #company.id ]
The people are shown on the company/view page and rendered with a partial. Note that the partial is in the views of 'people'
<%= render :partial => "people/person" , :locals => { :people => #people }%>
in the partial ...
<% for person in #people %>
...
<%end%>
<%= will_paginate #people %>
Now the partial does work, it renders all of the people and shows the paginate links on the bottom. However it doesn't not actually paginate the collection and instead it shows everything on the first page.
I am clearly missing something rather basic.
Thanks in advance.
Are you missing per_page?
Per_page should be the problem.
Also make :page => params[:page] look like :page => params[:page] || 1 so that will_paginate will stop complaining on blank page parameters.
Related
I have seen something similar asked before, but I can not understand it, and I can not ask for help there because of low reputation to comment.
So, I am asking a new question. I am using will_paginate plug in for two objects on the same page, and they are working but both move simultaneously. For example, if I click page 2 on the first, page 2 changes even in the second pagination.
This is my code in the controller:
#tasks = #student.tasks.paginate(page: params[:page], :per_page => 2)
#plans = #student.plans.paginate(page: params[:page], :per_page => 2)
And this is my code in the view:
<%= will_paginate #tasks %>
<%= will_paginate #plans %>
How can I make this work separately?
Your controller is using the same parameter :page for each model. Your view then uses that very parameter to set the page for both models in the view. You can define a different parameter to work with each model. ie.
#tasks = #student.tasks.paginate(page: params[:tasks_page], :per_page => 2)
#plans = #student.plans.paginate(page: params[:plans_page], :per_page => 2)
<%= will_paginate #tasks, :param_name => 'tasks_page'%>
<%= will_paginate #plans, :param_name => 'plans_page'%>
i am using will_paginate for pagination,
When i reach the last page and then click on Next link its again taking me to first page,
Actually i want to disable the Next button when i reach the last page.
please advise me how to solve this..
this is the code i used for pagination
<%= will_paginate #users,
:prev_label => "Preevious",
:next_label => "Next",
:page_links =>true,
:renderer => PaginationListLinkRenderer
%>
Controller:
#users = User.paginate :per_page => 5,
:page => params[:page],
:order => 'name ASC',
:conditions => ""
View:
<%= will_paginate #users,
:previous_label => "Previous",
:next_label => "Next",
:inner_window => "2",
:outer_window => "0" %>
Above is all my code and it works for me in will_paginate. Once reached down the last page, the "Next" link change to bolded text, so as the last page number (see image below).
I am using
<%= will_paginate #users %>
and it is working fine. It disables the next link when i reach the last page.
I have two partial. The first have a common will_paginate but in the second will_paginate I need to change the links (default url) generated by will_paginate.
Please I need their answer.
Thanks
You can use :param_name to change the query-string parameter:
<%= will_paginate #posts, :param_name => :posts_page %>
<%= will_paginate #comments, :param_name => :comments_page %>
Note that in your controller you must also change this:
#posts = Post.paginate :page => params[:posts_page]
#comments = Comment.paginate :page => params[:comments_page]
Try this
will_paginate(#some_collection, :params => { :controller => "foo", :action => "bar" })
Ok so i have a graphic model and I am using thinking sphinx as the search tool. It works well but i want to display different models on the search results page.. for example
i have this in my Graphic model
define_index do
indexes :name, :description, :scale,
indexes sub_category.name, :as => :subcategory_name
indexes sub_category.category.name, :as => :category_name
indexes colors.name, :as => :color_name
end
This is fine and good but the problem is i want to display all the categories and subcategories for a found search and not just the graphics that are related. In my controller should i have three find like
#graphics = Graphic.search params[:search]
#categories = Categories.search params[:search]
#sub_categories = SubCategories.search params[:search]
this seems like overkill...is there a better way so in the view i can show each of them seperately
You'll need to have indexes defined in your Category and SubCategory models as well, and then you can search across all three at once:
#results = ThinkingSphinx.search params[:search], :page => params[:page]
In your view, you'll want some logic around each search result to render the correct HTML - perhaps you can have different partials for each class? I'd also recommend wrapping it into a helper. Here's a start:
<ul>
<% #results.each do |result| %>
<li><%= render :partial => partial_for_search_result(result),
:locals => {:result => result} %></li>
<% end %>
</ul>
And the helper:
def partial_for_search_result(result)
case result
when Graphic
'graphics/search_result'
when Category
'categories/search_result'
when SubCategory
'sub_categories/search_result'
else
raise "Unknown search result/partial mapping for #{result.class}"
end
end
Hopefully this gives you some ideas on how to approach the problem.
Just to shorten example you can do:
in controller
#results = ThinkingSphinx.search params[:search], :page => params[:page]
in view
= render #results
should call every model partial 'graphic/_graphic.html.erb', 'categories/_category.html.erb' and so on
I am wondering if there is a way to complete this hack that I am trying to do with my Ruby on Rails app. I'm having trouble with some associations and I can't find the solution. You can see that question here.
In the mean time I am wanting to limit the display of records to a number like 10 but I don't want to do it in the controller through the :limit => 10 manner because I need to loop through all the records. I'm not even sure if this is possible but thought I would ask.
My view code is:
<% #comments.each do |comment| %>
<% if comment.workout.user_id == current_user.id %><br/>
<%= link_to (comment.user.username), comment.user %> <br/>
<%= time_ago_in_words(comment.created_at) %> <br/>
<%= link_to (comment.workout.title), comment.workout %><br/>
<%= sanitize(simple_format(comment.body), :tags => %w(p)) %>
<% end %>
<% end %>
My controller is simply calling
#comments = Comment.all(:order => "created_at DESC")
I would do all that in the controller. Views shouldn't be doing that logic. You say you don't want to only limit to 10, because you want all and do some filter. You can just:
#comments = Comment.all(:joins => :workout, :conditions => {:workouts =>{:user_id => current_user.id}}, :order => "created_at DESC", :limit => 10)
#comments[0..9]
and
#comments[10..-1]
Is the will-paginate gem the droid you're looking for?
After adding the gem to your environment.rb and installing it, you would modify your controller so that it calls paginate rather than find or all:
#comments = Comment.paginate(:all, :order => "created_at DESC", :per_page => 10)
And then in your view, add the pagination controls (links to advance a page, go back a page, &c.)
<%= will_paginate #comments %>