ruby-on-rails
2011-06-04 29 views 0 likes 
0

控制器代碼:如何在沒有得到「&」的情況下在RoR中輸出「&」?

class HelpController<ApplicationController 
    def index 
     @url = "https://example.com/auth?user_id=1234&redirect_to=http://google.ru" 
    end 
end 

查看代碼:

<script>location.href='<%[email protected]%>';</script> 

並重定向到這:

example.com/auth?user_id=1234&amp;redirect_to=http://google.ru 

此: http://example.com/auth?user_id=1234&放大器; redirect_to = http://google.ru (不含空格)

回答

2

在Rails 3中,您可以調用.html_safe方法告訴rails您已驗證內容安全地發送未轉義。

請參閱http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/以獲得對默認轉義行爲動機的解釋。

該成語與舊版rails相反,您必須明確調用.h(.html_escape)。

+0

我認爲它叫「safe_html」= D – AndrewShmig 2011-06-04 15:25:39

1

沒有Ruby專家,但我認爲轉義是默認行爲。你必須強制它通過將東西包裝在raw()中作爲非轉義字符串輸出。

相關問題