Rails, link to previous page without losing content - ruby-on-rails

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' %>

Related

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

page inbetween search results and results

So i have this page called ticket amount which has a bunch of buttons on it.
Wha im wanting is for the user to click the button on the row of the search results and be taken to that page. The user will then click on the amount of tickets they want and be taken to the page that matches the id from the from the row the button was on.
Basically at the moment its going
Search results >>>>> results
And i want
Search results >>>>> ticket amount >>>>> results
I've currently got the first one set up like this
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
How would i go about making it different? Basically i'm wanting to use the amount of tickets the user selects in the view. So probably best to change the url from this
/events/idnumber
to
events/idnumber/tickets_required=4
Any ideas how to do this?
At the moment the ticket_amount.html only has basic bootstrap buttons inside
What you need to do is to send the params in a redirect_to.
What I would do is a small form for let the user select the tickets_required, the form will point to an action in the controller, within that action I would add a redirect to the search results url sending the param:
#Within your controller action once the user selects the tickets_required
redirect_to event_path(event, tickets_required: params[:tickets_required])
Redirect to the search results url or event_path or whatever, and send the tickets_required param in the url using the param[:ticket_required] coming from the form submission
Take a look to this thread that will guide you about how to send the params in the redirect_to

Ruby on Rails: Entries per page

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.

ActionLink back button

I have an Index method that does double duty as showing a list of posts and a queried list of posts and can also have pages so you get urls like /News/Page/1 or /News?query=test
When a user clicks through to a post at say News/Details/1 they get a simple ActionLink that takes them back to the list. BUT I want this link to take them back to the actual page they were on in terms of the paging or the query. How could I do this? I don't want to use the JavaScript history method. Here is my current ActionLink: <%=Html.ActionLink("<< Back to News List", "Index")%> and this is an example of the paging links: <%= Html.RouteLink("<< First Page", "NewsPaging", new { query = ViewData["query"], page = 0 })%>
Thanks
It would probably be easier to just use javascript to send them back to the last page in their history (without creating a link to the specific page).
history.go(-1)
I will go for a ActionLink like
<%=Html.ActionLink("<< Back to News List", "Index")%>
because user can access News/Details/1 directly and if then you have a link that uses history.go(-1) or history.back() function it will not redirect it to the Index action
Another way to do this is to create the link in the controller and assign it to a property of the model or a simple ViewBag variable (up to you). For this example, I'm just going to use ViewBag.
So in your example, going from /News/Details/1 back to /News/Page/1?some-querystring, you can do the following:
In your controller (assuming '1' is the 'id'):
ViewBag.BackButton = String.Format("/News/Page/{0}{1}", id.ToString(), Request.Url.Query);
In your view:
< Back
One draw back to this is that you must know what the previous page was/could be. In your case, you want your back button to go back to your list, so this technique should be fine.

How to implement a search page which shows results on the same page?

I'm using ASP.NET MVC 2 for the first time on a project at work and am feeling like a bit of a noob.
I have a page with a customer search control/partial view. The control is a textbox and a button. You enter a customer id into the textbox and hit search. The page then "refreshes" and shows the customer details on the same page. In other words, the customer details appear below the customer search control.
This is so that if the customer isn't the right one, the user can search again without hitting back in the browser. Or, perhaps they mistyped the customer id and need to try again.
I want the URL to look like this:
/Customer/Search/1
Obviously, this follows the default route in the project.
Now, if I type the URL above directly into my browser, it works fine. However, when I then use the search control on that page to search for say customer 2, the page refreshes with the correct customer details but the URL does not change! It stays as
/Customer/Search/1
When I want it to be
/Customer/Search/2
How can I get it to change to the correct URL?
I am only using the default route in Global.asax.
My Search method looks like this:
<AcceptVerbs(HttpVerbs.Get)> _
Function Search(ByVal id As String) As ActionResult
Dim customer As Customer = New CustomerRepository().GetById(id)
Return View("SearchResult", customer)
End Function
A good place to start might be NerdDinner if you havn't already.
In the mean time though The approach I'd use is to have a page that has my search box on it.
Then I'd have a <div> that I name "SearchResults". This will ultimately hold my results to the search.
I then have a PartialView which takes a model that has all the search results and renders them.
So when I click the button I do a call out to a jQuery action that takes the search parameter, performs the search and then returns my PartialView as rendered HTML.
Back in the client side I take that rendered HTML and replace the contents of my div with the HTML.
The keywords to google, or SO, are RenderPartial. This is back end code to render a partial view and give you html.
Also jQuery postbacks so that you can call an action in your controller.
use RedirectToRoute action result
link

Resources