Put a Link in a Rails Notice

Sometimes you may want to put a link in your rails flash notice.  In my case I wanted to link to another site.

Here’s how.

In my controller:

notice: 'Click to go to <a href="www.yoursite.com">website.</a>'.html_safe

Notice the .html_safe function called on the string.

Credit here and here.

Advertisements

Customize Devise Flash Message Content in Rails

Devise automatically includes great flash messages, however sometimes you want to edit or make your own.  In my case I have customized the devise controller for registrations, and I want to call a flash message in my custom devise registrations controller. (refer to my post on how to override and create a custom devise controller here).

So by looking in the devise source code, in particular the registrations_controller.rb, we see how to do this. First, in the devise.en.yml file, create your custom flash message content in the right location. In my case I want it to be in the registrations controller, so in the devise.en.yml under registrations I created a new flash message like this:

registrations:
  wrong_beta_password: "You need the beta password to create an account." # this one is my custom one
  # Devise default flash messages here

Now, simply call this message in your custom devise controller:

set_flash_message :notice, :wrong_beta_password if is_flashing_format?

That’s it!