0

I want to be sure that the data I receive came from a specific page (it's for a navigator game).

I would prefer a solution using RoR but if we can do it with JS it's ok =)

Melki
  • 579
  • 2
  • 5
  • 26
  • You can find the name of the view. http://stackoverflow.com/questions/8846484/rails-access-view-name-inside-partial – ShaggyInjun May 29 '13 at 15:53
  • 1
    Or if you mean the page that referred you to the current page: http://stackoverflow.com/questions/5819721/how-to-get-request-referrer-path – flyingjamus May 29 '13 at 15:55
  • Its super easy to fake a referrer header so don't trust it to be actually reliable if you think someone may tamper with data. – Kyle May 29 '13 at 16:32
  • It's just a precaution I would regret to not take – Melki May 29 '13 at 16:49

1 Answers1

4

In your controller, you have access to the request variable (type of ActionDispatch::Request) which represents the actual request received by your server:

def index
  puts request.inspect # see in your server's console the output
  # ...
end

With this variable, you can access to the referer, which returns the path (as String) of the last page seen, or nil if you came from another domain (or blank page).


To check which page sent your form, you could use:

def my_controller_action
  if request.referer.present? && request.referer.include?('/string/to/check/')
    # yay!
  else
    # well, the request is not coming from there
  end
end

You could also set it as a before_filter in your controller in order to check "quietly" the requests and not in each controller's action.

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117