0

I have a ruby on rails app. I have a view that lists hyperlinks as such, this view is also rendered via same controller/action . when user clicks hyperlinks displayed on this page they go to the same controller/action, with a different id parameter. I want to be able to tell in my controller/view how user gets to these pages, i.e. whether user clicked on one of these hyperlinks vs user came through a different source?

<div id='list'>
  <a href='controller/action/1'> some link 1 </a>
  <a href='controller/action/2'> some link 2 </a>
</div>
ozil
  • 599
  • 7
  • 31

3 Answers3

1

Best approach is to add an URL parameter to the links.

<div id='list'>
  <a href='controller/action/1?via=from_view'> some link 1 </a>
  <a href='controller/action/2?via=from_view'> some link 2 </a>
</div>

Edit

You can access this URL parameter in your controller with params. Simple example:

class MyController < ApplicationController
  def show
    @via = params[:via] || "external"
  end
end
<% if @via == "from_view" %>
  <p>Hello coming from the view!</p>
<% elsif @via == "external" %>
  <p>Hello coming from external!</p>
<% end %>
<div id='list'>
  <a href='controller/action/1?via=from_view'> some link 1 </a>
  <a href='controller/action/2?via=from_view'> some link 2 </a>
</div>
  • thanks. that was my approach too, but only thing that made me consider was the URLs can be edited easily. I want to be able to look at the source where the user is coming from and display certain message in my haml file based on that. – ozil Jun 14 '20 at 16:32
  • As long as displaying this message is not a security concern there shouldn't be an issue with it. Can you give an example of these messages? – Christian Bruckmayer Jun 14 '20 at 21:23
  • thanks again. yeah the message is simply text , nothing that raises security concern. but how do I use the query value , in my controller or model or view , to make that decision to show the message or not. do I create another variable in my model? – ozil Jun 15 '20 at 13:04
  • You can access the query parameters via `params` in your controller and then pass it back to the view or a model depends what you need to do. See my edited answer. – Christian Bruckmayer Jun 15 '20 at 14:01
  • thanks. one follow up question. do i need to check this param for any security issues? – ozil Jun 16 '20 at 00:48
  • As long as you don't render this parameter directly in the view (html / js injection) or use it in a not prepared SQL statement (SQL injection) it shouldn't be necessary to do more checks. But it's hard to say yes/no without seeing the whole example. Just note that anyone can display / hide the message in your view with altering the URL parameter. – Christian Bruckmayer Jun 16 '20 at 06:14
0

You can easily pass URL parameters into Rails URL helper.

For example:

<%= link_to "Link Title", some_url_helper_path(:param1 => "value1", :param2 => "value2") %>

Then you will be able to pass the parameters from the view to the controller's action.

  • thanks, based on this source value, I want to display some content in the my haml file. is it possible by passing parameter this way? – ozil Jun 14 '20 at 16:30
0

Where the user came from is part of the HTTP spec (the referrer header) and you can access that in your controller with request.referer - you can then check if that value matches the page you want to target.

If your controller has @origin = request.referer You can then use if @origin == 'http://example.com/page-1’ in your view to switch what is being rendered.

This way you don’t have to remember to decorate all of your links with the extra param; rely on the fact that the browser is automatically adding it to headers for you.

Also see:

How to get request referer path?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer

Tom Harvey
  • 3,681
  • 3
  • 19
  • 28
  • thanks for your comment. I tried this and it works in the controller. another iteration ,i'm doing is adding presenter , where I can make a decision whether to render a particular partial view/js. is there a way that request.referer be available at presenter layer in ruby? – ozil Jun 16 '20 at 00:51
  • The values in the `request` object are usually only available in the controller. But, in the controller, assign the value of the referer to a variable; `@referer = request.referer` and then `@referer` will be available in your view layer. Have a look at how Christian’s answer passes the value of a param from the controller to the view using the `@via` variable for a fuller example. In fact, those answers are good - except from the idea of creating a param when the request headers already have the data you need. – Tom Harvey Jun 16 '20 at 01:00
  • You should also note that using the header instead of a query param in the url is much harder for the user to edit manually. You mentioned a concern about that in your comment. – Tom Harvey Jun 16 '20 at 01:59