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?
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)
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.”
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.
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With