Put a link in a flash[:notice]
I may be missing something obvious, but you should just be able to do
flash[:notice] = %Q[Created job number <a href="/jobs/list?job=#{update.id}">#{update.id}</a>]
and then just make sure you're not escaping the content of the flash when you display it in your view.
Add link to a flash notice in Rails ActiveAdmin view
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
Adding an External link in flash notice rails controller
Try it without the interpolation.It will work.
redirect_to root_path, flash[:success] = "Complete this quick survey. <a href='https://www.google.com'>Click here</a>".html_safe
or if you want to interpolate:
url = "https://www.google.com"
redirect_to root_path, flash[:success] = "Complete this quick survey. <a href='#{url}'>Click here</a>".html_safe
How to insert URL into flash messages?
You need to do two things: First, construct a flash message with a link in it, as you'd expect:
flash[:notice] = "Settings updated! <a href=\"#{root_path}\">Go home</a>."
Then, in your view, you'll need to echo it as raw content. This skips Rails 3's automatic escaping of tainted strings, so be sure you never pass user input through this flash:
<%=raw flash[:notice] %>
Is it possible to add link to the flash message when it's confirmed?
I had trouble implementing these solutions. All I needed was a simple HTML link in the flash. Here's how I implemented it in Rails 4.1. I just had to sanitize my flash messages in app/views/shared/_flash.html.erb:
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name %>">
<a class="close" data-dismiss="alert">×</a>
<% if msg.is_a?(String) %>
<div id="flash_<%= name %>"> <%= sanitize(msg) %> </div>
<% end %>
</div>
<% end %>
And in my controller, I just entered HTML directly and without .html_safe. Works a treat!
flash[:notice] = %Q[Please <a href="#">click here</a>]
I'm trying to implement `link_to` in a flash message for an update action in a controller in Rails but I cannot get it to render to the browser
You need to use html_safe
when outputting the flash messages - not when storing them.
<% flash.each do |key, msg| -%>
<%= content_tag :div, msg.html_safe, class: name %>
<% end -%>
.html_safe
just sets a flag on the string object that its trusted and should not be escaped.
The flash works by storing flash messages in the session storage - by default this means a cookie in the browser.
So when you do:
flash[:notice] = "foo"
You're storing the raw string "foo" in a cookie* and its unpacked back into the session on the next request. But the string is not the same Ruby object - so the html_safe
flag on the string object is not persistent.
Add a line break in a flash notice rails controller
There is always a dirty way to do stuff in programming. Here you can create multi line array inside flash.
flash[:success] = []
flash[:success] = flash[:success] << "We will notify you when we go LIVE! "
flash[:success] = flash[:success] << "Meantime, complete this quick survey so we can get to know your interest."
and display it like this
<% flash.each do |key, value| %>
<% value.each do |text| %>
<%= content_tag :div, text %>
<% end %>
<% end %>
This will definitely work.
But the best way to do that is this
flash[:success] = "We will notify you when we go LIVE! <br> Meantime, complete this quick survey so we can get to know your interest.".html_safe
If this also doesn't work then try it another way
in controller set
flash[:success] = "We will notify you when we go LIVE! <br> Meantime, complete this quick survey so we can get to know your interest."
and in view display like
<% flash.each do | value| %>
<%= content_tag :div, value.html_safe %>
<% end %>
Related Topics
Activerecord - Querying Polymorphic Associations
How to Send Email via Smtp with Ruby's Mail Gem
How to Generate a Human Readable Time Range Using Ruby on Rails
Differencebetween 'After_Create' and 'After_Save' and When to Use Which
Ruby on Rails: How to Edit Database.Yml for Postgresql
Sorting a Ruby Array of Objects by an Attribute That Could Be Nil
Csv.Read Illegal Quoting in Line X
Rails 4 Migration: How to Reorder Columns
Encoding::Undefinedconversionerror
How to Find the Unique Elements in an Array in Ruby
Rails and Os X: How to Install Rmagick
How to Get Sinatra to Refrain from Adding the X-Frame-Options Header
Understanding How Establish_Connection Works in Activerecord
Os X Mountain Lion: Gcc-4.2 No Such File or Directory
Why Do This Ruby Object Have Both To_S and Inspect Methods That Appear to Do the Same Thing