gpt4 book ai didi

ruby-on-rails - 来自 Rails 4 中 Controller 的带有 html_safe 的 Flash 消息(安全版本)

转载 作者:数据小太阳 更新时间:2023-10-29 06:41:17 24 4
gpt4 key购买 nike

在我的 Controller 中,我有以下代码:

    format.html { redirect_to new_customer_url,
notice: %Q[ A customer already exists with with this shopping id. Edit this customer #{view_context.link_to("here", edit_customer_url(@duplicate))}.
].html_safe

我希望能够在 Flash 消息中包含一个链接,因此(如您所见)我调用 html_safe 来取消转义该字符串。然而,从 Rails 4.1 开始,这似乎有了不同的处理方式。 (参见 herehere)

this question 中提供了解决方案.但是,它只能通过将 html_safe 调用移动到 View 来实现,具有取消转义所有 flash 消息的效果。

我宁愿比这更偏执一点,有什么办法可以将链接包含在来自 Controller 的 flash 消息中吗?

最佳答案

这是解决此问题的一种可能方法。向您的 ApplicationController 添加一个前置过滤器,这将使 flash[:notice] 仅在设置了 flash[:html_safe] 时才安全。然后您可以控制何时以及何时不从 Controller 完全安全地制作通知 html。

before_filter -> { flash.now[:notice] = flash[:notice].html_safe if flash[:html_safe] && flash[:notice] }

然后你的例子可以修改成这样:

format.html do
redirect_to(
new_customer_url,
notice: %Q[ A customer already exists with with this shopping id. Edit this customer #{view_context.link_to("here", edit_customer_url(@duplicate))}.],
flash: { html_safe: true }
)
end

关于ruby-on-rails - 来自 Rails 4 中 Controller 的带有 html_safe 的 Flash 消息(安全版本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com