View helper for mongoid-pagination? - ruby-on-rails

In rails, does mongoid-pagination have a view helper that will automatically render the pagination links on a view on which it is applied? Do I have to build my own one instead.
For example with Kaminari you can use <%= paginate #pets %> on your view and you get the pagination links ( Prev 1 2 3 Next ).

Kaminari can actually support Mongoid. Maybe you can explore that area.

Related

Need to exclude pre and next buttons from kaminari pagination

I want to change kaminari pagination paginate helper format like
First,Prev - current page - Next,Last
Because its breaking my design with unwanted numbers.
I tried with all other helpers but no success.
Is there any way? Please let me know your thoughts.
Here's my thoughts. You need to go to the Kaminari GitHub page. That link leads to section about generating Kaminari partials, so you can edit them just like you want. Right from that page:
rails g kaminari:views default -e haml
Where haml is your template engine. You can replace it by erb, slim (depending on what you prefer to use).
Update. Here's a related question about customizing Kaminari templates.
Use Kaminari's themes
<%= paginate #users, :theme => 'my_custom_theme' %>
you need custom kaminari view files in
app/views/kaminari/my_custom_theme
And another simpler solution, what happen if you do this?
<%= paginate #users, :window => 0 %>
You can customize it a little more using CSS, each part of the pagination widget has some class or id, you can hide/show/modify them
[vidur#centos7-demo tukaweb]$ rails g kaminari:views default -e erb
Running via Spring preloader in process 25960
create app/views/kaminari/_first_page.html.erb
create app/views/kaminari/_gap.html.erb
create app/views/kaminari/_last_page.html.erb
create app/views/kaminari/_next_page.html.erb
create app/views/kaminari/_page.html.erb
create app/views/kaminari/_paginator.html.erb
create app/views/kaminari/_prev_page.html.erb
in the view:
<%= paginate #data %>

rails rendering partial with collection / other class is in charge?

Somewhat new to rails, longtime programmer. I've got a question about views, controllers and partials really - wondering if I have this setup well.
I've got a pages controller, and on the index page (really the pages index method) I've got a partial in layouts called featured (ie app/views/layouts/_featured.html.erb) -- I've also got a Featured class. I would like basically the index of the featured class to be drawn here. But of course it's not working. SO the question is:
In the page itself I've got the <%= render 'features/index' %> which I'm beginning to think is the wrong way to go..
Do I axe this partial method and just call <%= render 'features/index' %> and let everything progress natively or
What would be the proper way of routing the featured collection to the partial? Since the controller is actually Pages it seems like I'm fighting against the tide.
<%= render 'features/index' %>
Doing this is wrong given your description. This will try to render a partial from app/views/features/_index.html.erb which you haven't mentioned.
To render the partial at app/views/layouts/_featured.html.erb you would do (perhaps a bit more verbose that is necessary)
<%= render partial: "layouts/featured" %>
The best suggestion I can offer is to pass a collection to this partial
<%= render partial: "layouts/featured", locals: { features: #features } %>
Since it seems your intention is for this partial to appear as a piece of a layout I will assume you wish for this partial to appear on multiple pages. This means on multiple actions you will need to have assigned the set of Feature instances this #features instance variable. One way to do this is a before_action.
before_action :setup_features
# ...
private
def setup_features
#features = Feature.all
end
A good place to start learning more about filters is in the Rails Guide
The partial at "app/view/layouts/_featured.html.erb" can only be rendered with
render 'featured'
and not 'featured/index'
render 'featured/index' will render "app/views/layouts/featured/_index.html.erb
Since the pages controller is really rendering the main index page in it's def index, all I had to do was #features = Feature.all and the variable is available for the partial pulled into the index page.
I need to get used to how simple rails is coming from other languages / frameworks.

rails 3,Kaminari pagination for an simple Array

For paginating a common array I got this solution,
#arr_name =
Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
PER_PAGE_RECORDS is a variable with value as per needed for pagination.
Any better Ideas??
Also to have an ajax call for using pagination one can use this,
In your view,
give id to your div tab
div id="paginate"
and inside it
<%= paginate #arr_name, :remote => true %>
And in js response file put,
$('#paginate').html('<%= escape_javascript(paginate(#arr_name, :remote
=> true).to_s) %>');
So your requests will be AJAX.
Thanks.
This is the only available helper method to paginate an array object using Kaminari. Another alternative is, as suggested solution in kaminari wiki page, add the instance methods to the array object.
If you are trying a common solution based on the ActiveModel return type ( .all returns array and .where returns ARL) then following is an workaround.
unless #arr_name.kind_of?(Array)
#arr_name = #arr_name.page(params[:page]).per(PER_PAGE_RECORDS)
else
#arr_name = Kaminari.paginate_array(#arr_name).page(params[:page]).per(PER_PAGE_RECORDS)
end

Rails 3 Pagination for NON-ActiveRecord results

My rails 3 app uses sphinx to perform search on text files, and I return a collection of results on the landing page view: something like
<%= render :partial => "result", :collection => #results %>
What is the Rails way to do pagination for the results? I know there are plugins like will_paginate, but I'm not using ActiveRecord (my app doesn't even use a db).
Is a Javascript solution the best way to go? I want to also maintain REST in the url, ie "/search?query=hello&page=2"
Here is someone who has already done so: How to use will_paginate with non-ActiveRecord collection/array

Help me understand dynamic layouts in Sinatra

Help me understand this; I'm learning Sinatra (and Rails for that matter, er, and Ruby).
Say I'm doing a search app. The search form is laid out in one div, and the results will be laid out in another. The search form is rendered into the div by a previous view (maybe from a login form).
I want to process the form params, perform the search, and render the results into the results div.
If I have a single "yield" in the layout and render the divs from different views, the results div erases the search div when it renders.
If I define the divs in the default layout, then just render the content, obviously the layout will be messed up: there would have to be two "yields" and I don't think Sinatra supports passing blocks in to yields.
I tried foca's sinatra-content-for plugin, and that seems closer to what I need. But I can't figure out where to place the "yield_content" statements.
If I have this haml in my layout:
#search
-# search form
= yield_content :search
#results
-# search results
= yield_content :results
... this in my search view:
- content_for :search do
%form{:method => "post"... etc.
... and this in the results view:
- content_for :results do
%table{:class => 'results'... etc.
This sort of works but when I render the results view, the search div is emptied out. I would like to have it remain. Am I doing something wrong? How should I set this up?
I think you mean you ALWAYS want to show 2 divs, but in a new search they should be empty and on a results page they should be populated. You can probably get away w/ one haml template, and just populate it diff'tly on the request method:
get "/search" do
# render haml
end
post "/search" do
# set instance variables: #search & #results
# run search
# render haml
end
(Sorry this is very pseudo... not at a real computer.)

Resources