Letting a user navigate to a /show page under certain conditions - ruby-on-rails

I would like a user to be able to get to matchups/:id/show, however, only when they search for the matchup using the site's built-in search functionality, not when they input the url directly in the navigation bar. The search function uses a controller action that finds the matchup they are looking for and redirects_to to the matchup's show page.
The reason for this is that users vote on things on the site, and every time they vote they get a credit to spend towards a search. I want them to be able to search for a matchup, but not to navigate to it on their own, therefore avoiding the need to earn credits.
Right now, the search function finds the matchup they are looking for and simply does:
redirect_to #matchup
so my attempt at putting a before_action filter on the controller's show method did not work, because it also prevented the user from accessing the matchup's show page when they did use the search function.
Is this possible? Thanks!

You can check the referrer for getting this done. When user enters the URL in the navigation bar, it won't have any referrer.
You can do it in your controller.
if request.referer
#came (probably) from search
else
#came by entering url in navigation bar
end
However, here request.referer will exist if user comes from any page rather than search page. If you want to do something ONLY IF they came from search page, you've many options including:
Add a cookie
Add a query string to the URL to identify which page it referred from
You can check in request.referer if it matches your acceptable pattern.

Related

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

Redirect visitors to a page saved in cookie

I have a main navigation and
Visitors click the main navigation -- a cookie remembers their last page.
Next time visitors enter the site from the root url they are redirected to that page.
How would you do it?... Any code would be appreciated :)
Save the last location they visited like this, you could probably do this with a simple before filter on your ApplicationController. In it you can add some logic to decide if you want to save the new location or retain the current one.
cookies.permanent[:last_visited_path] = request.request_uri
Then in the action pointed to by your root URL:
if cookies[:last_visited_path].present?
redirect_to cookies[:last_visited_path]
end
You could also look into signing the cookie with cookies.permanent.signed if you want to prevent users from tampering with it.

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

request.referer with anchor

is there a way to get the referer with the anchor from the referer page..
the situation is - i have a search page which loads results (users) below the search box using ajax. i am changing the hash/anchor on the fly when the search keyword is typed in.
so it becomes http://localhost/users/search#foo
the user gets link in the ajax result to edit the user. i want the user to come back to this same search result after he is done editing the user.
Anchors are not included in the referrer. You'll need to use query parameters or define the return URL (with the anchor) in the session before updating the user.

Resources