Kaminari pagination, how to get the number of the current page? - ruby-on-rails

I am using Kaminari to paginate some results from a db query.
I would like to apply specific styling to the first page of the results.
The operation is very easy once I know on what page the user is, but I can't find a way to detect the current page.

If you want to check for first page you can do it like this:
if object.first_page?
#your logic here
end
If you find the specific page you can do something like this:
current_page_no = object.current_page
For more info refer: http://www.rubydoc.info/github/amatsuda/kaminari/master/Kaminari/PageScopeMethods

Have you generated the partials that kaminari uses?
See the section titled "Customizing the pagination helper" here:
https://github.com/amatsuda/kaminari
Oncey ou do that, you can edit the _paginator.html.erb file (or other kaminari partials if you need to) in order to get the pagination functionality you're looking for. These partials allow you to use local variables, like "current_page", "num_pages" and a few more. Sounds like that's what you're looking for.
Here's what those partials look like, if you want to see them before running the generator:
https://github.com/amatsuda/kaminari/tree/master/app/views/kaminari
Hope that points you in the right direction.

2020 Update. As of now I am able to do #users.current_page.

Related

Spree Commerce: display multiple categories on one page

I'm trying to make a link that can open or go to other page that display several categories on one page, like this: mugs, bags, and books
Is it possible to achieve this without using deface or modifying any page?
Maybe a normal link like:
https://www.example.com/t/categories/mugs,bags,books
https://www.example.com/products?taxon=1,3,5
I've tried both of them. I get error from the former and get the first category from the latter.
If deface is the only solution, how to do it if I want the link to look like the first link?
If it helps, I use rails 4.2.1 and spree 3.0.1.
I don't think you get this sort of functionality by default in Spree. You're going to have to come up with a custom action that is capable of accepting the multiple categories and returning the products filtered by that category.
You should be able to set this up by through the admin panel in
"Products" --> "Taxonomies"
If you edit "Categories" and add subcategories of "Mugs", "Bags" and "Books" you should be able to get what you are after via
https://www.example.com/t/categories/

What is the Rails way to split a view into separate pages?

Within my show action/view I'm displaying a lot of data that I want to split-up into separate pages (in this case three pages total). I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
Great question!
I can do this easily by adding a new action and view for each additional page, but is that the "correct" way to do it in Rails?
I suspect you are unsure about violating REST?
I don't know what data you are displaying, but In the end the clearest and simplest solution for you (code wise) and your users (design wise) should win, if that means adding a new action, so be it. Avoid adding a new controller just for the sake of a new show action.
Are you looking for a pagination solution? If so, I would suggest either kaminari or will_paginate. Also, they each have railscast if you need any help getting set up.

How to set an initial value for my object in ruby on rails

I am still pretty new to Rails and am working on a basic app and have run into a problem, which I can't seem to get around, sorry if this question has already been answered, but searching didn't yield me any results.
Basically I am creating an app that catalogues all of someones's clothes, Now the main page is the index page of everything they have logged, and each picture is linked to the show page where more information is revealed. I want to use AJAX to bring that show page to the side of the main index page. I am working off of another tutorial to figure this out but have run into a problem. On the initial load of my index page, I need to define a variable for that show page so that it can be rendered using a partial, but I can't use find(params[:id]) since the user hasn't selected anything yet. So my question is how do I set that initial value to either just the first item in the catalogue or not have anything appear, and then when someone does click the link, the sidebar shows the more detailed description of the piece of clothing.
Thanks for any help.
#object = params[:id] ? MyModel.find(params[:id]) : MyModel.first
But I think there's some problem with design of application.
You might have some luck working with the ruby gem 'PJAX'. Here is a screen cast showing you how to get that AJAX sidebar you want. Good luck
It sounds like you can just print the container element as normal, but leave it empty when the page is generated. Optionally, hide it via CSS. Then, when you load its content with AJAX, set it to visible or just populate it as normal.
Alternatively, if you really want to set it to the first item in the catalog (or in any ActiveRecord) you can use .first e.g. Products.first, and use that value to populate its initial contents.

Ruby On Rails: Fetching records

I'm looking for a tutorial or a piece of code that can leads me to create a page
where we can find only 10 data rows per page and navigating with Ajax previous/next arrows
and also page number (i.e.: < 1 2 3 ... 10 > ).
Thank you very much.
A good place to start is Mislav's will-paginate plugin. The Readme at that link should explain how to use it.
Edited:
There's also a screencast on RailsCasts about Ajax pagination. I haven't watched it, but his stuff is usually really high-quality and easy to follow.
it's not ajax but maybe it's useful for you... when you use haml and haml_scaffolding in rails it's authomatic the pagination using this kind of sacffolding

How do I tell ActiveScaffold to always show the search form in a list view?

The ActiveScaffold list view has a search form that is loaded via ajax when a user click the search link. I'd prefer to have the form show by default when a user opens a list page.
I've figured out a way to trigger the ajax call when the page loads, but I'm wondering if there's a way to get ActiveScaffold to render the form automatically. Is there a template or a method I can override? I've had a look through the code but there's nothing obvious, at least to me.
Update:
srboisvert's answer inspired me to have a better look.
The trick is to use Template overrides to refactor the following: list.rhtml, _list_header.rhtml, _search.rhtml so that the search form partial renders inline.
There is a way to get it rendered automatically:
active_scaffold :model do |config|
config.list.always_show_search = true
end
I don't currently have an active scaffold project handy but here is how I would figure it out.
I'd use firefox with firebug installed and take a look at what is called when the link is clicked. Then I would go look at that javascript and what it is generating. Then I would search the source for any part of the code or combination that would be fairly unique to the search box ajax. After that it should be easy to cut and past it in without the ajaxyness.
The option
config.list.always_show_search = true
works fine, but only on concrete controller. It throws an exception when used in AS set_default block. Somebody know better solution then to include it in every controller (apart from overriding the template which is handy but complicates version updates)

Resources