Ruby on Rails: Entries per page - ruby-on-rails

I have a search page that allows the user to search on many different fields. After clicking search the page displays 10 entries per page by default, with the option to view the next 10, or pick a page number, using will_paginate. There is a drop down menu that allows you to pick how many entries the user sees per page.
I have tried two pieces of code:
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]), { :onchange => "this.form.submit();"} %></br>
and
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]), :onchange => "'#{request.query_string}&per_page=' + this.getValue()" %></br>
When the first drop down is changed to 20 for example, the page instantly searches again, but displays the results 20 per page.
The second drop down allows you to select a entry per page number, but to change the view, you have to re click search.
I want my code to work more like the first drop down, but I think re searching is a bit expensive. Is there a way to stop resubmitting the search but change the display?
Thanks in advance

If you change the amount of items displayed per page, a requery has to be done anyway (because only the items to display have been loaded from the database). And if you change the items displayed per page, and you are on page 2, what will you see? Items 10 - 60? So if you change the items per page, you almost always also switch to page one, and that requires a requery anyway.
And second: it seems you want to reload the data in the javascript. You do a call to get the data, but seem to do nothing with the loaded data.

Related

How to render users pagination when an action is performed on a certain user

We render users in alphabetical fashion with 10 entries per page using will_paginate gem
Assuming we perform an action to activate or deactivate a user in the 2nd page (say user15)
, We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here
There is another case where this is becoming problematic:
Assuming there are 31 entries, so 4th page will contain a single entry
, If we delete this user, we can't show the fourth page since no such user exists
In the above scenario, page=4 and per_page=10, but it would fail to render this page
How should we handle these scenarios, should we just show page=1 in any of the above scenarios.
What would be the right thing to do in the above scenario
We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here
Can you elaborate on this one please.
Assuming there are 31 entries, so 4th page will contain a single entry , If we delete this user, we can't show the fourth page since no such user exists In the above scenario, page=4 and per_page=10, but it would fail to render this page
You might want to look into cursor pagination. Instead of pages, you have a cursor for the record and fetch everything before / after this cursor. This cursor could for instance be the encoded created_at timestamp.
Cursor pagination has a lot of other advantages over offset pagination too. However, will_paginate does not support cursor pagination so not sure if this is a feasible approach for you.
To get around these performance concerns, some database engines offer cursor-based pagination, or encourage pagination based on min/max values, e.g. WHERE id > :max_id in which the :max_id value is based on the previous page of results. This approach is usually superior for speed and memory concerns, but comes with some tradeoffs; the most notable being that it's only ever possible to go to the next page of results and not immediately jump to page 20, for example. The will_paginate library does not handle cursor-based pagination.
https://github.com/mislav/will_paginate/wiki
https://medium.com/#meganchang_96378/why-facebook-says-cursor-pagination-is-the-greatest-d6b98d86b6c0

page with params between search and show page

So i have this page that shows a result of events with a button that takes you to the eventid (which has a list of prices)
What im wanting to do is one of my api's is only showing all tickets available (how considerate). So i'm having to adjust to them.
What im wanting (and have made but not put in yet) is to have a page inbetween the search results and the show.html.erb
This page has a bunch of buttons on it, Asking how many tickets your looking for. e.g. 2 tickets for the event.
Now what im wondering is how i can go about this?
Currently the button is linked up like this (on the search results page)
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
However what im going to want to do is have this button link off to a page called ticket_numbers.html.erb This will sit inbetween these two pages and when a user clicks said button on that page it shall allow me to use the number in the view and related jquery files.
Any help would be great!!
Thanks
Sam
You need to define a route for this new page:
match '/ticket_numbers' => 'controllerName#ticket_numbers'
And to create the file ticket_numbers.html.erb on the corresponding view folder.After this your new page will work.
If you need any controller logic define a method ticket_numbers on the corresponding controller:
class controllerNameController < ApplicationController
def ticket_numbers
end
end

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.
I have a basic application created by generating scaffold.
There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.
I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.
Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..
As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):
<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>
Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

collection_select does not reload the item after page reload

i have the following code for collection_select
<%= f.collection_select :customer_id, Customer.all, :id, :name, options ={:prompt => "-Select a Customer"}, :style=>'width:210px;'%><br />
Problem :-
If i select a customer and then refresh the page, the collection_select does not refresh the list but shows the previously selected item.
How can i ensure that when i refresh/reload the page even the collection_select gets refreshed? guidance required (sorry for the bad English)
This is browser specific. Modern browsers can remember your choices upon a soft refresh and pre-select them. Generally a re-submission of the url should be enough to ensure a clean form is displayed.
Ctrl+F5, like Sachin asked you to do, generally removes the browser cache for that particular page before refreshing and hence works as you have seen.

Rails, link to previous page without losing content

My rails app has a basic search function that queries the database and displays a list of active records. The search results are displayed as links. When the user clicks on a link for a given search result they will be taken to a page with details of their selection and what not. I want to have a back button on this details page that will take the user back to the search results page with the search results still showing up. The way it is now the back button takes the user back to the search results page but all of the search results are gone.
You have to pass the query parameter(search text) to the details page where you have the back button.
Include the query parameter in the back button url as http://localhost:3000/search?query=foo
Capture search query information in params[:query] and take it from one page to another.
In result detail page add this:
<%= link_to 'Back', "#{:back}+#{params[:query]}", :class => 'button' %>

Resources