Rails 2.3.7 的 content_for 问题
我有一个应用程序,我想使用“content_for”渲染菜单以及左侧导航栏,然后其余部分是我的文档的正文。然而,在我的左栏和菜单区域中,html 标记被转义。我看到一篇关于在产量之前使用 raw 的帖子,但这没有帮助。
在花了相当长的时间尝试在真实的应用程序中诊断它之后,我创建了一个非常简单的测试,并且能够用最少的代码让它完成相同的任务。这是我所拥有的:
layouts.application.erb
<body>
<section id="page">
<header>
<nav class="clear"><%= raw yield :navigation %></nav>
</header>
<%= yield %>
</section>
</body>
</html>
pages/index.html.erb
<% content_for :navigation do %>
<ul><li><%= link_to 'New page', new_page_path %></li></ul>
<% end %>
<h1>Listing pages</h1>
<table>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
<% @pages.each do |page| %>
<tr>
<td><%=h page.title %></td>
<td><%=h page.body %></td>
<td><%= link_to 'Show', page %></td>
<td><%= link_to 'Edit', edit_page_path(page) %></td>
<td><%= link_to 'Destroy', page, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
当我在浏览器中点击页面时,导航部分没有正确转义(实际上是链接,但不是列表,这对菜单来说很重要:))
这是来自全新的 Rails使用脚手架生成的代码的项目,该代码仅被修改以将默认链接移动到导航部分(并添加该部分)。
I have an application where I want to render the menu using "content_for" as well as a left navigation bar and then the rest be the body of my document. However, in my left bar and menu regions, the html markup is being escaped. I saw a post regarding using raw before the yield, but that didn't help.
After spending quite some time trying to diagnose it within the real app, I created a VERY simple test and was able to get it to do the same with minimal code. Here is what I have:
layouts.application.erb
<body>
<section id="page">
<header>
<nav class="clear"><%= raw yield :navigation %></nav>
</header>
<%= yield %>
</section>
</body>
</html>
pages/index.html.erb
<% content_for :navigation do %>
<ul><li><%= link_to 'New page', new_page_path %></li></ul>
<% end %>
<h1>Listing pages</h1>
<table>
<tr>
<th>Title</th>
<th>Body</th>
</tr>
<% @pages.each do |page| %>
<tr>
<td><%=h page.title %></td>
<td><%=h page.body %></td>
<td><%= link_to 'Show', page %></td>
<td><%= link_to 'Edit', edit_page_path(page) %></td>
<td><%= link_to 'Destroy', page, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
When I hit the page in a browser, the navigation part is not escaped properly (actually the link is, but not the list, which is kinda important with menus:) )
This is from a brand new rails project using scaffold generated code which was modified only to move the default link up into the navigation section (and adding that piece in).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您搁置内容时(而不是显示内容时)会发生转义。尝试在
content_for
中使用raw
:The escaping is happening when you set aside the content, not when you display it. Try using
raw
withincontent_for
: