2013-05-29 23 views
0

我想确保我收到的数据来自特定页面(这是一个导航游戏)。如何查看哪个页面发送了表单?

我宁愿使用RoR的一个解决方案,但如果我们可以用JS做它的确定=)

+0

您可以找到视图的名称。 http://stackoverflow.com/questions/8846484/rails-access-view-name-inside-partial – ShaggyInjun

+1

或者如果你的意思是指你到当前页面的页面:http://stackoverflow.com/questions/5819721/how-to-get-request-referrer-path – flyingjamus

+0

thanks @ user1598086它有很大的帮助 – Melki

回答

3

在你的控制器,您可以访问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为了检查“悄悄”的请求而不是每个控制者的行为。

相关问题