How to create a view_all_page using Kaminari Pagination - ruby-on-rails

I am trying to create a view_all_page using Kaminari.
I have successfully installed the gem kaminariand using the kaminari THEME in twitter bootstrap.
I would like to create a view_all_page.html.erb to add to my app/views/kaminari theme.
Everything works great so far with my pagination, I just need to add a link/method to view all.
In my projects_controller.rb, under def index, I have #projects = Kaminari.paginate_array(#projects).page(params[:page]).per(10)
In my index.html.erb page I have <%= paginate #projects %>
In my app/views/kaminari folder I have _first_page, _gap, _last_page, _next_page, _page, _paginator, and _prev_page (.html.erb files)
I am looking to possibly customize the pagination helpers or figure out some logic to view all in my projects_controller.rb

You could set the params[:pagesize] to the count of all projects. Your view and controller might look something like this:
index.html.erb
<%=link_to "Show All", projects_path(:show_all => true) %>
projects_controller.rb
def index
params[:pagesize] = params[:show_all] ? Projects.count : 10
#projects = Kaminari
.paginate_array(#projects)
.page(params[:page])
.per(params[:pagesize])
end

Related

Rendering paginated pages in JBuilder views

I am currently paginating the return of a query attendees that has over 9000 items. My pages and routing work fine but I would like them to appear at the bottom of the page as clickable links to that page of the results. I am relatively new at using JBuilder I am using the Kaminari gem as well as the API-Pagination gem and would like to know how to I add visible/clickable page numbers to a JBuilder view according to Kaminari Docs <%= paginate #attendees %> is all that is needed. But as far as I understand JBuilder does not work or interpret that logic as its purely manufacturing JSON objects? Any advice is appreciated as well as a better explanation of what JBuilder is doing.
Controller
module Reports
class ConferencesController < ::ApplicationController
def attendees
#conference = Conference.find(attendee_params[:conference_id])
#attendees = #conference.attendees
paginate json: #attendees, per_page: 500
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.conference #conference, partial: 'conference', as: :conference
json.attendees #attendees, partial: 'attendee', as: :attendee
<%= paginate #attendees %>
Kaminari works great of the box for HTML partials, but there are some additional things you need to do to set it up for other response formats. You can remove the paginate json: #attendees, per_page: 500 line from your controller in favor of something like
#attendees = #conference.attendees.page(params[:page]).per(500)
Additionally you will need to provide additional information to your jbuilder partial to render this information.
Something like this:
json.meta do
json.total_pages #attendees.total_pages
json.total_count #attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope

Ruby on Rails layouts and rendering

I'm new to RoR and I'm a little bit confused with Rails MWC. I feel like I misunderstand something.
For example, I want to have home page where I could render top 5 articles and top 5 products. Products and articles have no relations at all, it is totally separate data.
So what I try to do is, i crate 2 sacffolds products and articles, and 1 controller for home page. I root to homepage controller. Then in homepage template i try to render products and article template. I get an error that methods which are used in products and articles controllers are undefined.
I don't understand where is problem. Is this kind of template rendering one template inside another is not Rails convention. Or I have bugs in my code.
I don't see your code but in this case I'm quite sure you have bugs in it.
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
#products = Product.top5 # Your logic to fetch top 5
#articles = Article.top5
end
end
app/views/home/index.html.erb
<% #products.each do |product| %>
<%= product.name %>
<% end %>
<% #articles.each do |article| %>
<%= article.name %>
<% end %>
This is perfectly fine, I've done that multiple times. Consider that in Rails you don't have any relation between controller and models, there are convention but Rails controller is not bound at all to any model
First, you need to instantiate your variables #products and #articles (this is an example) on your controller method. Then you can render the view.
Pay attention to add an # before. Only variables with an # will be available on your rendering view.
By default, when you call a GET for /products you'll arrive on the index method. At the end of this method, if not any view is specified, Rails will render views/products/index. In this view, you'll access all variables instantiate with an # and do whatever you want with.
First, yes, a template rendering another controller's template (not a partial) is not within Rails conventions.
A scaffold is a "single-resource" controller: it takes your model definition and generates a basic controller for editing and displaying that particular model (i. e., Product).
What you really need to do is use the two models you've generated in the home page controller, kinda like this:
class HomePageController < ApplicationController
def index
#articles = Article.top_5
#products = Product.top_5
# Render the #articles and #products in the view.
end
end

Rails will_paginate - how to make nicer route?

I have on the homepage list of articles that are listed with using will_paginate gem.
Everything works well, but there is one thing I would want to improve - on the homepage, the data are loaded into home controller and index action.
So, when I load my website - www.website.com, data are loaded into home/index. When I click on the pagination link for the second page, the link is www.website.com/home/index?page=2. I would like to see there in the best way www.website.com/page/2 (or www.website.com/?page=2).
Basically, the point is to remove from the URL /home/index - is there any way to do this?
Thanks
You may do it this way - add this class to some helper module, for example app/helpers/application_helper.rb:
module ApplicationHelper
class SmartLinkRenderer < WillPaginate::ActionView::LinkRenderer
protected
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target.gsub(/[?&]page=(\d+)/,'/page/\1')
tag(:a, text, attributes)
end
end
end
You may customize it according to your needs, for example do
attributes[:href] = target.gsub(/home\/index/,'')
or whatever. And then you may do this in your views:
<%= will_paginate #items, :renderer => 'ApplicationHelper::SmartLinkRenderer' %>
Try this
root to: 'home#index'
in you config/routes.rb file
EDIT
to be able to route these kind of requests:
www.website.com/page/2
add this to routes.rb:
match "page/:page" => "your_root_controller#index"
this way :page will be in your params hash.
and for example index method to view your Message model can be:
def index
#messages = Messages.paginate(page: params[:page])
end
hope that can help. for more information please refer to RoR routing

Rails rendering a different view depending on the controller

I have a tab navigation page in my rails app which is shared across all of my views. Inside I have a small text area which should change depending on the page that the user is on.
Currently I am doing this by adding a variable to the controller and using it in the render partial path, like so:
class Myapp::WebsitesController < MyappController
def set_up
#page = 'websites/left_text_info'
end
and then in my partial:
<%= render :partial => #page %>
This works but it doesn't feel like the best 'ruby' way of doing things. Can anyone advise on a better way of doing this?
Thanks
You can use controller_name helper method directly in your view and skip the controller part:
<%= render "#{controller_name}/left_text_info" %>
Or if the only thing that change is the content of the textarea, then perhaps the best way is to define a helper method that returns only the content for it, so you don't need multiple partial files that are very similar.
module ApplicationHelper
def text_area_content
case controller_name
when "users"
"content for users"
when "articles"
"content for articles"
else
"other content"
end
end
end

Rails pagination of subset of a class

I have a rails app and I'm trying to set up pagination for a view of the Essays class. I'm new to rails... so I can do this for ALL of them, but I only want certain ones to be in this list (where all the essays are contained in Essay.find(Ranking.where(:user_id=>current_user.id).essay_id)).
home.html.erb contains (among other things):
`<%= will_paginate #essays%>
<ul class="users">
<%= render #essays %>
</ul>
<%= will_paginate #essays%>`
in the Pages Controller:
def home
#...
#essays = Essay.paginate(:page => params[:page])
end
I tried adding #essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id) before the #essays=Essay.paginate(:page => params[:page]) but the method essay_id for the Ranking class is not available here. How do I get around this? Thanks!
This should work:
Essay.joins(:rankings)
.where(:rankings => {:user_id => current_user.id})
.paginate(:page => params[:page])
While this can be done with will_paginate. I've had some issues with this plugin for Rails 3. A much smoother solution, in my opinion, was to use another pagination plugin called Kaminari.
Assuming, essay_id is passed as a param, you could try:
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => params[:essay_id]).page(params[:page]).per(10)
Or depending on your logic. If the essay object has already been identified in your controller:
#essay = Essay.find(1234)
#ranking = Ranking.where(:user_id => current_user.id, :essay_id => #essay.id).page(params[:page]).per(10)
And then, in your view:
<%= paginate #ranking %>
Even better, to get you started on Kaminari, you can view this rails cast. As noted from the rails cast:
The first-choice gem for pagination in
Rails is will_paginate, but the
currently released version doesn’t
support Rails 3. There is a
pre-release version available that
works but it hasn’t been updated for
several months. If will_paginate is no
longer in active development are there
any other gems we could use?
One alternative is Kaminari. This
seems to provide a cleaner
implementation of pagination and
offers several improved features, too,
so let’s try it in our application
instead.
Hope that helps!
Simply chain paginate method after find method:
#essays=Essay.find(Ranking.where(:user_id=>current_user.id).essay_id).paginate(:page => params[:page])

Resources