将字符串标记为 HTML 安全
我正在尝试构建我的第一个 Rails 应用程序,并且使用 Ryan Heath 的 navigation_helper 插件为我提供当前的我的导航中的类。我构建了我的命名路由,如下所示:
match 'games' => 'games#index', :as => :games
match 'new' => 'games#new', :as => :new
match 'previous' => 'games#previous', :as => :previous
match 'settings' => 'settings#index', :as => :settings
然后在我的 application_layout 中添加了以下代码
<%= navigation([:games, :new, :previous, :settings]).html_safe %>
根据我对 Rails 的了解, html_safe 应该强制 HTML 正确呈现,但我得到的是这样的:
<ul class="navigation">["<li class=\"current\"><a href=\"/games\">Games</a></li>", "<li class=\"\"><a href=\"/new\">New</a></li>", "<li class=\"\"><a href=\"/previous\">Previous</a></li>", "<li class=\"\"><a href=\"/settings\">Settings</a></li>"]</ul>
那么我是做错了什么还是插件做错了什么?我知道这个插件是在 2.x 天内写回来的,据我所知,它处理 HTML 的方式有点不同,但我只是知道得不够。
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">["<li class=\"current\"><a href=\"/games\">Games</a></li>", "<li class=\"\"><a href=\"/new\">New</a></li>", "<li class=\"\"><a href=\"/previous\">Previous</a></li>", "<li class=\"\"><a href=\"/settings\">Settings</a></li>"]</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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
https://github.com/priceflex/navigation_helper/commit/ad7bf45db1845e9299e9da39cf214866b608dd47尝试使用这个fork来修复rails3的问题
https://github.com/priceflex/navigation_helper/commit/ad7bf45db1845e9299e9da39cf214866b608dd47 try to use this fork wich fix issues for rails3
您可以使用 raw() 方法来避免转义:
You can use raw() method to avoid escaping: