Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add link to the flash message when it's confirmed?

I'm using Devise.
When the user succeeded at confirmation, the flash message appears.
I'd like to add a link to it.

So I want this message instead

Your account was successfully confirmed. You are now signed in. Go to your profile page, and edit it!

Then the part of profile should be the link to example.com/users/username/edit

How can I make it possible?

devise.en.yml

confirmations:
  confirmed: 'Your account was successfully confirmed. You are now signed in.'
like image 367
HUSTEN Avatar asked Mar 24 '13 10:03

HUSTEN


2 Answers

You can use the method view_context to access any view method inside your models and controllers.

For example:

def index
  flash[:notice] = "Go to your #{view_context.link_to("profile page", link_path)}, and edit it!"
  redirect_to link_path
end

Update link_path accordingly.

like image 151
weltschmerz Avatar answered Nov 13 '22 05:11

weltschmerz


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">&#215</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>]
like image 4
bryanus Avatar answered Nov 13 '22 04:11

bryanus