Rails 4,flash消息中的链接未解析为HTML

Rails 4,flash消息中的链接未解析为HTML,html,ruby-on-rails-4,escaping,Html,Ruby On Rails 4,Escaping,在控制器中,我有以下闪光信息: flash[:notice] = %Q[Please #{view_context.link_to('create a new account', new_account_path)}, after that you will be able to create orders.].html_safe 以下是布局中的闪光区域: <div id="main_flash_area"> <% flash.each do |name, msg| %>

在控制器中,我有以下闪光信息:

flash[:notice] = %Q[Please #{view_context.link_to('create a new account', new_account_path)}, after that you will be able to create orders.].html_safe
以下是布局中的闪光区域:

<div id="main_flash_area">
<% flash.each do |name, msg| %>
    <div class="alert text-center alert-info">
      <a class="close" data-dismiss="alert">&times; Закрыть</a>
      <%= msg %>
    </div>
<% end %>
</div>

,之后您将能够创建订单。
生成的HTML:

<div id="main_flash_area">
    <div class="alert text-center alert-info">
      <a class="close" data-dismiss="alert">× Закрыть</a>
      Please &lt;a href="/accounts/new"&gt;create a new account&lt;/a&gt;, after that you will be able to create orders.
    </div>
</div>

× Закрыть
请a href=“/accounts/new”创建一个新帐户/a,之后您将能够创建订单。

如何使其成为正确的链接?我猜它会在某个时候逃逸标签。

您需要在视图中对闪存
进行消毒
msg

这将在视图中呈现链接,而不是转义html

请注意,这将适用于应用程序中的所有flash消息。如果您在flash消息中显示任何用户输入,您必须记住在显示之前先将其转义,因为Rails自动转义将不适用


请注意,
sanitize
帮助程序对
raw
帮助程序的权限较低,可以对其进行配置。它会自动与链接一起工作,默认情况下会删除
脚本
标记,如果flash中有用户内容,则会提供一些保护,但您需要进行全面检查,以确保不会引入任何安全问题。查看Rails文档了解更多信息。

非常感谢您的解决方案nmott

这里为未来的读者提供了更多的上下文,包括您的解决方案,因为我不知道如何使用它

在views/layout/application.html.erb中,注意清理(msg)以呈现html

<body>
   <%= render 'layouts/header' %>
  <div class="container">
  <% flash.each do |name, msg| %>
  <%= content_tag(:div, sanitize(msg), class: "alert alert-info") %>
<% end %>
<%= yield %>
</div>
</body>

太好了,这正是我需要的!有没有办法在不清除所有flash消息的情况下完成此操作?以下是一些关于为什么会发生这种情况的上下文,以及关于如何修复的讨论:
<body>
   <%= render 'layouts/header' %>
  <div class="container">
  <% flash.each do |name, msg| %>
  <%= content_tag(:div, sanitize(msg), class: "alert alert-info") %>
<% end %>
<%= yield %>
</div>
</body>
def update
    respond_to do |format|
      if @review.update(review_params)
        format.html { redirect_to @item, notice: "Review was successfully updated. #{view_context.link_to("review", item_review_path(@review.item, @review))}" }
        format.json { render :show, status: :ok, location: @item }
      else
        format.html { render :edit }
        format.json { render json: @item.errors, status: :unprocessable_entity }
      end
    end
  end