Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Last active May 13, 2023 15:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zulhfreelancer/ca6ef3fbb41ac235b231adecb33e495f to your computer and use it in GitHub Desktop.
Save zulhfreelancer/ca6ef3fbb41ac235b231adecb33e495f to your computer and use it in GitHub Desktop.
How to add link inside Rails flash message? Tested with Rails 6.
<!--
* File: app/views/shared/_flash.html.erb
* Note: the `html_safe` is important here
-->
<% flash.each do |type, msg| %>
<div class="alert alert-info">
<%= msg.html_safe %>
</div>
<% end %>
<!--
File: app/views/layouts/application.html.erb
-->
<!DOCTYPE html>
<html>
<head>
<title>Your App</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_pack_tag 'application' %>
</head>
<body>
<%= render "shared/flash" %>
<%= yield %>
</body>
</html>
class YourController < ApplicationController
def your_method
# Other logic here
# Note that we are using "view_context" here
flash[:error] = "Click #{view_context.link_to 'here', something_path} to learn more"
redirect_to other_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment