I have a view that shows two Active Record queries, one of which I use will_paginate on.
All works great with will_paginate, but if I click next or page 2, the second query is not longer rendered on the view.
I probably don't understand the flow of will_paginate processing, so any help is appreciated. In particular, why is will_paginate affecting the other, unrelated query from showing in the view? If I click on the first page, the 2nd query shows up again in the view. The controller returns the two queries and has the .paginate on the one query.
Thanks!
Per request, here's a summary of the code I'm asking about. I forgot to mention the other query also uses will_paginate. And therefore, I found my problem. I was using the same page parameter for both paginates! I found a good article describing what to do:
http://candidcode.com/2009/11/03/paginating-multiple-models-using-will_paginate-on-the-same-page/
And updated my code, and it works. This is basically, the correct code when you want two will_paginates on different models on the same view.
Controller:
def index
#query1 = Post.somescope.paginate(:page => params[:query1_page], :per_page => 50)
#query2 = Post.someotherscope.paginate(:page => params[:query2_page], :per_page => 20)
end
View:
<ul class="list-unstyled">
<li>
<%= will_paginate #query1, :param_name => 'query1_page' %>
</li>
<% #query1.each do |q1| %>
<li>...</li>
<% end %>
</ul>
<ul class="list-unstyled">
<li>
<%= will_paginate #query2, :param_name => 'query2_page' %>
</li>
<% #query2.each do |q2| %>
<li>...</li>
<% end %>
</ul>
Related
I have set-up will_paginate to paginate an array (list of customer orders) and am using the following code in my show view (the orders are nested on the seller's page);
<ol>
<% #seller.trans.paginate(:page => 1, :per_page => 6).each do |buy| %>
<li><%= buy.customer.name %>£<%= buy.sum %><%= buy.date %></li>
<% end %>
</ol>
This has placed the right restriction on the array (6 per page) and I can manually filter through by changing the page number to 1, 2 or 3 (I have 13 orders) but the 'Next/Previous' links are missing from the view.
What is it I'm doing incorrectly? Thanks.
i think the code should be something like this
<ol>
<% #buys = #seller.trans.paginate(:page => 1, :per_page => 6) %>
<% #buys.each do |buy| %>
<li><%= buy.customer.name %>£<%= buy.sum %><%= buy.date %></li>
<% end %>
</ol>
<%= will_paginate(#buys) %>
take a look at the man page of the will_paginate gem from here, https://github.com/mislav/will_paginate
Okey, so this is how I would go about it:
View:
<%= will_paginate #seller, renderer: BootstrapPagination::Rails %>
Controller:
#seller = *Model*.paginate(:page => params[:page], :per_page => 6);
This has been tested using the gem will_paginate-bootstrap
Good luck!
I have a comment model under a micropost. I was wondering if I can paginate this so that 50 comments won't show all at once once the comment button is clicked on the micropost. I am pretty sure this cannot be paginated but is there a way that like facebook, only a certain amount shows and then the user can show more if they please. Currently my code looks like this:
Micropost HTML for the Comment Section
<div id='CommentContainer-<%= micropost.id%>' class='CommentContainer Condensed2'>
<div class='Comment'>
<%= render :partial => "comments/form", :locals => { :micropost => micropost } %>
</div>
<div id='comments'>
<%=render micropost.comments %>
</div>
</div>
When a button above is clicked it shows this section and I would not like 50 comments to show up all at once. All suggestions will be very helpful. Thank you!
Make sure you are calling #comments = Comment.paginate(:page => params[:page], :per_page => 10) in your controller
You render the group through the <%= will_paginate #comments %> call and not through :render and then later through will_paginate
Since I already know you are using nested partials. I would make sure that the item you are calling has the correct collection. After those three things are met, it should be working fine.
You can also be sure to checkout the railscast on will_paginate although without a pro subscription you may have to watch the outdated version.
I would suggest you to lookup 2 rails gems that are most widely used for pagination on rails
--> will paginate
--> Kaminari
I prefer Kaminari over will paginate because it offers a lot more flexibility than will paginate.
I've just started to use Partials in my rails application, at the moment i have the following code in my application.html.erb
<%= render 'categories/categorieslist' %>
This links to _categorieslist.html.erb in my views/categories/ folder
At the moment this partial contains hard coded hyperlinks
<ul class="unstyled">
<li style="padding-bottom:5px">Item A»</li>
<li style="padding-bottom:5px">Item B»</li>
</ul>
My aim is to have these categories coming from the database, e.g
<ul class="unstyled">
<% #categories.each do |category| %>
<li style="padding-bottom:5px"><%= category.name %> » </li>
<% end %>
</ul>
I have tried adding a categorieslist method in the categories controller e.g
def categorieslist
#categories = Category.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #categories }
end
end
but this is not being called by the partial (and i don't feel this is even the correct way to do it), and is showing the error
NoMethodError in Store#index
on the line <% #categories.each do |category| %>
My question is how do i pass into the partial in the application.html.erb file, the categories object that usually would come from a controller method in the categories controller?
Any help would be great.
You can send locales with your partial call in your view and pass variables to that partial.
For example (this is a partial shortcut):
Your view from which you call the partial
<%= render 'categories/categorieslist', :all_categories => #categories %>
Your partial categories/_categorieslist.html.erb (note there is no # with the variable)
<ul class="unstyled">
<% all_categories.each do |category| %>
<li style="padding-bottom:5px"><%= category.name %> » </li>
<% end %>
</ul>
For further information (and the long version), see 3.4.4 Passing Local Variables in the Rails Guides.
I'd use a collection for this:
<%= render 'categories/categorieslist', :collection => #categories, :as => :category %>
This renders a collection of items. In this case, all the categories. You can also pass it a custom name with the :as => .
Then in your partial you only include the stuff you want the items in the collection to render:
<li style="padding-bottom:5px"><%= category.name %> » </li>
The -ul- isn't included as it would be rendered multiple times. You'll need to wrap it around your render tag.
The result is the same as the suggestion #timbrandes outlined, (check out docs he linked to).
I've heard :collection gives you performance improvements.
http://rails-bestpractices.com/posts/38-use-render-collection-rails-3-when-possible
That's not the right way to do it, and your question is rather confusing.
I'd say that you still have to read a rails book. You seem to be still a bit too fresh
Anyway, controller methods usually represent http requests. And they are invoked accordingly to what is defined in the config/routes file. Views (*.erb) do not usually invoke controller methods. If they do so, they do it through an ajax request.
Data is passed from actions to the views through controllers instance variables.
If you want to invoke any methods within the views, they should be defined in helpers. Still, the only data they will manipulate is the one passed from controllers as instance variables.
I have this method at the moment:
<% for comment in #critical_process.review.comments %>
<div id="comment">
<%=h comment.comment %> <br/>
<span id="comment_email">
By: <%=h comment.user.email%>
</span>
</div>
<% end %>
however I need it to display the comments in order of the most recently updated at the top and work its way down.
Thanks
Assuming the Comment model has an updated_at column, and you are using Rails 3, you can just tell ActiveRecord to order the Comment records appropriately as follows:
<% for comment in #critical_process.review.comments.order('updated_at DESC') %>
The Rails 2.x equivalent would be:
<% for comment in #critical_process.review.comments.all(:order => 'updated_at DESC') %>
Whilst this will work perfectly well, it's generally considered best to move most of your query generation into the controller, in which case you might do the following in the controller:
#comments = #critical_process.review.comments.order('updated_at DESC')
... and then iterate over the #comments collection in the view.
I saw this post
Ruby on Rails - Awesome nested set plugin
but I am wondering how to do the same thing without using node? I am also wondering what exactly this node is doing in the code itself.
In my categories view folder i have _category.html.erb and a _full_categores_list.html.erb.
The _category.html.erb has the following code which is the sae way as the link above.
<li>
<%= category.name %>
<% unless category.children.empty? %>
<ul>
<%= render category.children %>
</ul>
<% end %>
</li>
The _full_categories_list.html.erb has the following code.
<ul>
<% YourModel.roots.each do |node| %>
<%= render node %>
<% end %>
</ul>
This code works perfectly fine. However, lets say hypothetically that I wanted to create duplicates of these files so instead of _full_categories_list.html.erb I was maybe making a _half_categories_list.html.erb which might do something a little different with the code.
If I use similar code like what i used above in the _full_categories_list.html.erb it will keep calling the _category.html.erb.
How can I show all the cats, sub cats, and sub sub cats by using _half_categories_list.html.erb and a file like _half_category.html.erb instead of _category.html.erb
The half category and full category are just names to indicate that I am doing something different in each file. I hope this makes sense. I want to basically duplicate the functionality of the code from the link above but use _half_category.html.erb instead of _category.html.erb because I'm trying to put different functionality in the _half_category.html.erb file.
First: there's a simpler way to write _full_categories_list.html.erb using render, with the :partial and :collection options.
<ul>
<%= render :partial => :category, :collection => YourModel.roots %>
</ul>
This is equivalent to the _full_categories_list.html.erb you wrote above.
roots is a named scope provided by awesome_nested_set. You can add more scopes to your models - for example a named scope called half_roots (see the link for information about how).
With this in mind, _half_categories_list.html.erb could be written as follows:
<ul>
<%= render :partial => :half_category, :collection => YourModel.half_roots %>
</ul>
You can then use _half_category.html.erb to render the categories in that special way you need.