Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add link to a flash notice in Rails ActiveAdmin view

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 revision 65156) [x86_64-linux]

eg: If I try to do

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

The flash message is displayed as,

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

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

like image 663
gisha gireesh Avatar asked Apr 09 '19 08:04

gisha gireesh


1 Answers

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/14d6e500c777e82111faafe9392d90a6efed7e0b/lib/active_admin/views/pages/base.rb#L86

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
like image 192
chumakoff Avatar answered Nov 11 '22 17:11

chumakoff