如何查看哪个页面发送了表单?

时间:2013-05-29 15:50:40

标签: ruby-on-rails forms

我想确保我收到的数据来自特定页面(这是导航游戏)。

我更喜欢使用RoR的解决方案,但如果我们可以使用JS,那就ok =)

1 个答案:

答案 0 :(得分:4)

在您的控制器中,您可以访问request变量(类型ActionDispatch::Request),该变量代表服务器收到的实际请求:

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

使用此变量,您可以访问referer,它返回上次显示的页面的路径(作为字符串),如果您来自另一个域(或空白页面),则返回nil


要查看发送表单的页面,您可以使用:

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

您还可以将其设置为控制器中的before_filter,以便“安静地”检查请求,而不是检查每个控制器的操作。