2013-05-29 22 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爲了檢查「悄悄」的請求而不是每個控制者的行爲。

相關問題