在Rails ActiveAdmin视图中添加指向Flash通知的链接 [英] Add link to a flash notice in Rails ActiveAdmin view

查看:73
本文介绍了在Rails ActiveAdmin视图中添加指向Flash通知的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Active-admin控制器成功创建操作时在Flash消息中添加链接,但是该链接的HTML被转义并在视图上显示为纯文本。

I am trying to add a link in a flash message on successful create action of active-admin controller.But the HTML of the link is being escaped and displayed as plain text on view.

Rails 5.2.1,ruby 2.5.3p105(2018-10-18修订版65156)[x86_64-linux]

Rails 5.2.1, ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]

例如:如果我尝试do

eg: If I try to do

flash[:success] = "Complete this quick survey. <a href='#{url}'>Click here</a>".html_safe

Flash消息显示为

The flash message is displayed as,

"Complete this quick survey. <a href='https://www.example.com'>Click here</a>"

预期行为:Flash消息中应该有可用的链接

Expected behaviour: There should be a working link in the flash message

推荐答案

达到目的的唯一方法(可能不是一个好方法)是覆盖 activeadmin 的方法。 :build_flash_messages 方法可生成Flash消息。

The only way (probably not a good one) to achieve your purpose is to override activeadmin's :build_flash_messages method which generates flash messages.

https://github.com/activeadmin/activeadmin/blob/14d6e500c777e82111faafelib/active_b2/active_admin pages / base.rb#L86

这是覆盖的版本(添加了 .html_safe )。将此代码放在初始化程序中的某个位置。

This is the overridden version (added .html_safe). Place this code somewhere in initializers.

class ActiveAdmin::Views::Pages::Base
  def build_flash_messages
    div class: 'flashes' do
      flash_messages.each do |type, message|
        div message.html_safe, class: "flash flash_#{type}"
      end
    end
  end
end

这篇关于在Rails ActiveAdmin视图中添加指向Flash通知的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆