Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML in Rails flash messages

Tags:

I use flash[:error] to display a simple message to users that they cannot do a delete operation under certain conditions. I also have a link that will help them to get information about the entity that they wanted to delete and why they cannot do so.

Is it advisable to include this hyperlink in the flash message? Which would mean that I would have a HTML fragment in my controller. If not, how would I go about doing this?

like image 213
Vijay Dev Avatar asked Oct 29 '10 11:10

Vijay Dev


People also ask

What is flash Ruby Rails?

A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. Example messages: “Password changed correctly” (confirmation)

What is a flash message in Web?

Flash messages are a means of communicating messages to the end user from inside of an application. These messages might be errors, warnings, or success types of messages. Some examples of flash messages are: “You have been successfully logged out.”

What is flash message in mobile?

Flash SMS is a distinct kind of text message that appears instantly on the screen of a mobile device without requiring the user to take any action in order to read it. Even if the screen is locked, a flash SMS, also referred to as a class 0 SMS, will display on the smartphone.


2 Answers

If you want to include a link in your flash message from the controller there are 2 issues. Generating the link and then getting it displayed as HTML.

To use the link_to helper in the controller, fully qualify it.

To have the string display as html (instead of being escaped), call the html_safe method on the string. So the line in your controller might look like:

flash[:error] = "You can't do that. #{ActionController::Base.helpers.link_to "Here's why.", '/more_info.html'}".html_safe 
like image 157
Ritchie Avatar answered Sep 21 '22 17:09

Ritchie


the flash object is a holder for storing view fragments/messages and persist them for one redirection using the session. I see absolutely no problem in storing a link, or better an URL.

example :

redirect_to posts_path, :alert => "You cannot do that", :flash => { :url => post_path(@post) } 

and in layout view, the usual suspects :

- if flash[:alert]   ...   - if flash[:url]     = link_to "blah blah", flash[:url] 
like image 29
Nicolas Blanco Avatar answered Sep 19 '22 17:09

Nicolas Blanco