Marking a string as HTML safe

筅森魡賤 提交于 2019-12-10 11:47:45

问题


I am trying to build my first Rails application and I'm using Ryan Heath's navigation_helper plugin to give me the current class in my navigation. I built my named routes as follows:

match 'games' => 'games#index', :as => :games
match 'new' => 'games#new', :as => :new
match 'previous' => 'games#previous', :as => :previous
match 'settings' => 'settings#index', :as => :settings

Then in my application_layout I added the following code

<%= navigation([:games, :new, :previous, :settings]).html_safe %>

From what I know of Rails the html_safe should force HTML to be rendered properly, but instead what I get is this:

<ul class="navigation">[&quot;&lt;li class=\&quot;current\&quot;&gt;&lt;a href=\&quot;/games\&quot;&gt;Games&lt;/a&gt;&lt;/li&gt;&quot;, &quot;&lt;li class=\&quot;\&quot;&gt;&lt;a href=\&quot;/new\&quot;&gt;New&lt;/a&gt;&lt;/li&gt;&quot;, &quot;&lt;li class=\&quot;\&quot;&gt;&lt;a href=\&quot;/previous\&quot;&gt;Previous&lt;/a&gt;&lt;/li&gt;&quot;, &quot;&lt;li class=\&quot;\&quot;&gt;&lt;a href=\&quot;/settings\&quot;&gt;Settings&lt;/a&gt;&lt;/li&gt;&quot;]</ul> 

So am I doing something wrong or is the plugin doing something wrong? I know that the plugin was written back in 2.x days which from what I know handled HTML a bit differently, but I just don't know enough.


回答1:


https://github.com/priceflex/navigation_helper/commit/ad7bf45db1845e9299e9da39cf214866b608dd47 try to use this fork wich fix issues for rails3




回答2:


You can use raw() method to avoid escaping:

<%= raw(navigation([:games, :new, :previous, :settings])) %>


来源:https://stackoverflow.com/questions/7300901/marking-a-string-as-html-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!