I have too list #comments and #eval in the same view. If I go to the page 2 in the #eval, the comments page go to the page 2 as well, there is a simple way to do this in the separated way?
You can define a custom parameter name (param_name) for each pagination link group and differentiate the page number by that:
# in view
<%= will_paginate #foos, :param_name => :foo_page %>
<%= will_paginate #bars, :param_name => :bar_page %>
# in controller
#foo = Foo.paginate(page: params[:foo_page])
#bar = Bar.paginate(page: params[:bar_page])
Find the list of possible options in the source code of the gem: https://github.com/mislav/will_paginate/blob/v3.0.5/lib/will_paginate/view_helpers.rb#L46
Related
I'd like to enable signed-in users the ability vote on items and visit a page that lists all their votes in a paginated format. Currently clicking on the pagination links doesn't update the content, though will_paginate accurately knows how many links to render.
In other words if there are 15 voted on items, and will_paginate specifies outputting 5 items per page, I will see 3 will_paginate links. However clicking on the link doesn't update the content (I'm stuck on page 1). Any help would be much appreciated thanks.
In Controller
require 'will_paginate/array'
def myfaves
#user_likes = current_user.find_liked_items.paginate(:page => params[:page], :per_page => 5)
end
In View
<%= will_paginate #user_likes %>
I figured out the issue, in View when rendering items to the page I had to call
<% #user_likes.each do |songs| %>
This ensures
<%= will_paginate #user_likes %>
is tied to the items rendered on the page.
I'm trying to render a partial based on the taxon the user is inside. In my application.html.erb layout I have the following line of code:
<%= render 'spree/shared/women_subnav' if #enable_women %>
In the taxons controller, inside the show method, I have:
#taxon_id = params[:id].split('/').first
And in taxons#show I have:
<% if #taxon_id == params[:id].split('/').first %>
<%= "#enable_#{#taxon_id}" = true %>
<% end %>
When I run this I get a SyntaxError. But in taxons#show If I just enter:
<% if #taxon_id == params[:id].split('/').first %>
<%= "#enable_#{#taxon_id}" %>
<% end %>
without the '= true' then the page renders, outputting '#enable_women'. So I know it's getting the correct variable, I just need that variable to be set to true. What am I missing?
Thanks so much.
First of all I would like to give you some heads-up:
calling first on a user submittable input is not a great idea (what if I submit ?id=, it would return nil) also non utf-8 encoding will crash your app such as: ?id=Ж
Controllers are beast! I see you are setting the value of a true/false instance_variable in the view, please use controllers do define the logic before rendering its output. especially when parameter dependant.
so for a solution:
in your controller as params[:id] should suggest an INT(11) value:
def action
# returning a Taxon should be a good idea here
#taxon = Taxon.find(params[:id])
# as I would give a Taxon class an has_many relation to a User
#users = #taxon.users
end
and in your action's view
<%= render :partial => "taxons/users", collection: #users %>
of course you would have the great ability to scope the users returned and render the wanted partial accordingly.
if you want more info about "The Rails way" please read:
http://guides.rubyonrails.org/
Have fun!
use instance_variable_set
instance_variable_set "#enable_#{#taxon_id}", true
just a reminder that it's better to do these things inside a controller.
I'm creating a search page that will do an application wide search on users, posts, and comments. I currently have:
# POST /search
def index
query = params[:query]
#users = User.search(query).page(params[:page])
#posts = Post.search(query).page(params[:page])
#comments = Comment.search(query).page(params[:page])
respond_to do |format|
format.html
end
end
However I'm really trying to get something where all the results are mixed together then paginated. What are some of the strategies for doing paginated search like this? Thanks!
Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128
You can do the following
In your view you can do this:
<%= paginate #users, :remote => true, :param_name => "user_page" %>
<%= paginate #posts, :remote => true, :param_name => "post_page" %>
<%= paginate #comments, :remote => true, :param_name => "comment_#{some_post_id}_page" %>
and then in your controller you can refer to them in this way:
#users = User.search(query).page(params[:user_page])
#posts = Post.search(query).page(params[:post_page])
#comments = Comment.search(query).page(params[:comment_page])
and your view's js.erb you might have something like:
$('#posts').html('<%= escape_javascript render(#posts) %>');
$('.table-pager').html('<%= escape_javascript(paginate(#posts, :remote => true).to_s) %>');
Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:
#results = #users + #posts + #comments
#results.sort! { |a, b| a.score(query) > b.score(query) }
Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.
Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:
User.multi_solr_search query, models: [Post, Comment]
You could combine the results from the query and run page on that.
users = User.search(query)
posts = Post.search(query)
comments = Comment.search(query)
#results = users + posts + comments
#results.page(params[:page])
Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari
All goes well up until the point or running the server.
controllers/stories_controller.rb
def index
#stories = Story.all
#pages = Story.page(params[:page]).per(3)
#stories = Story.search(params[:search])
end
views/stories/index.html.erb
<%= paginate #pages %>
When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last »). What am I missing to get the pagination working?
I still can not understand your code. Why do you assign Story.all to #stories in the 1st line and overwrite the variable in the 3rd line?
Anyways, #stories will display "all the stories from the DB" because you're not calling the pagination method (.per) on #stories. The pagination links will show you the paginated counts because you're calling per method on #page variable and passing it to the helper.
I mean, you need to call .per on the relation before passing it to <%= paginate %> helper.
It's quite simple.
I guess you want to get results from your search, right?
Try
#stories = Story.search(params[:search]).page(params[:page]).per(3)
and something like:
<% #stories.each do |story| %>
<%= render story %>
<% end %>
<%= paginate #stories %>
in your view
I am new to rails so go easy. I have built my first blog and would like to set it up such that in the <% post.each do |post| %> render of the posts, I would like it to work such that it only displays 10 posts and then has a link to view the next 10.
I am hoping this is an easy question. Let me know if you would like me to post any code.
You should use the gem will_paginate.
The installation and usage is really easy: https://github.com/mislav/will_paginate/wiki/installation
Example usage: http://github.com/mislav/will_paginate
will_paginate is definitely the way to go, I just thought I'd add a link to a railscasts will_paginate video showing you how to use it since sometimes that can be an easier way to learn the basics than reading documentation. Plus you'll learn a brief history on how pagination used to be done when it was an included part of Rails (it was slower and more cumbersome). The old classic pagination has been moved out into it's own plugin too, but it's only recommended if you were already using it when you upgraded Rails to a version where they took it out.
Railscasts has a ton of other great videos that you might find helpful. For example, once you get more advanced you might want to try ajax pagination
Check out the will_paginate Gem.
It’s very easy to do pagination on ActiveRecord models:
#posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
In the view, page links can be rendered with a single view helper:
<%= will_paginate #posts %>
I've got problems using "will_paginate", installing the GEM then unistalling... a REAL PROBLEM! So I decide to program the pagination on RoR, it was not difficult so I wanted to share what I did:
Controller:
def index
#how many register per page i want to show
#b = 30
#params via GET from URL
a = params[:page].to_i
#first register per page
a1 = params[:page].to_i * #b
#the query can be easy...
#callcenters = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :order => "created_at DESC",:limit => "#{a1},#{#b}", :select => "radcheck.username as clave,caller_id, created_at, value")
#I need to know how many pages somehow
#registrostotales = Income.all(:joins => "LEFT JOIN radcheck ON radcheck.id = incomes.radcheck_id", :select => "radcheck.username as clave,caller_id, created_at, value").count
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #callcenters }
end
end
View:
...
Total de registros: <%= numero = #registrostotales %><br/><br/>
<!-- total pages -->
<% pages = #registrostotales / #b %>
<!-- the last page -->
<% if pages % #b > 0 %><% pages = pages + 1 %><% end %>
Paginas:
<% (0..(pages-1)).each do |i| %>
<!-- href or link_to, http://xxxxxxxx/yyyy?page=i -->
<%= link_to i+1, :action => "index", :controller => "callcenters", :page => i %>
<% end %>
<!-- the view.. -->
<table>
<tr>
<th>#</th>
<th>Teléfono (ID)</th>
<th>Zona</th>
</tr>
<% #callcenters.each do |callcenter| %>
<tr>
<td><%= numero - params[:page].to_i * 30 %><% numero = numero - 1 %></td>
<td><%= callcenter.caller_id %></td>
<td><%= Node.first(:conditions => {:calling_id => callcenter.caller_id}).description %></td>
</tr>
<% end %>
</table>
I hope this helps to someone!
We can do this with ease by using 'will_paginate-bootstrap' gem.
To continue firstly you add a line to the gem file as,
gem 'will_paginate-bootstrap'.
Now run bundle install.
In the controller, you will be using like #post = Post.all to get the
contents from models.
Instead use this
#post = Post.paginate(:page=>params[:page],per_page:5)
Here in the above line per_page field is to specify how many records you need
in a page.
In index.html.erb
At the end of the code i.e before ending the body tag
Add this,
<%= will_paginate #post,renderer: BootstrapPagination::Rails %>
Note: I thought Post as modal and post as variable declared in the controller.
Just go with your variables.
You can use the kaminari gem.
Very ease to use and highly customization friendly.
Adding pagination to a Ruby on Rails app
To add pagination to your Ruby on Rails app, you have to modify the following files:
Gemfile:
gem 'will_paginate', '~> 3.1.0'
gem 'will_paginate-bootstrap'
Areas controller --> index
#areas = Area.pagination_request(params[:page])
index.html.erb
<%= will_paginate #areas, renderer: BootstrapPagination::Rails %>
Model file:
def self.pagination_request(page)
paginate :per_page => 10, :page => page
end
Or, small, swifd & actively maintained: Pagy