Marking a string as HTML safe

2019-06-11 02:54发布

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.

2条回答
趁早两清
2楼-- · 2019-06-11 03:15

You can use raw() method to avoid escaping:

<%= raw(navigation([:games, :new, :previous, :settings])) %>
查看更多
登录 后发表回答