Marking a string as HTML safe

2019-06-11 03:18发布

问题:

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])) %>