I have a controller like this in my project:
class BriefcasesController < ApplicationController
...
def update
@super_power = SuperPower.find(params[:super_power_id])
@briefcase.contents.delete(params[:super_power_id].to_s)
flash[:notice] = "Successfully removed #{view_context.link_to(@super_power.title, super_power_path(@super_power)} from your briefcase."
redirect_back(fallback_location: '/briefcase'
end
end
The link_to helper is not rendering a link to the browser, rather it's printing the html: Successfully removed <a href=\"/powers/1\>flying</a> from your briefcase. I've also tried using the method #html_safe on the flash message to no avail. I'm wondering if there's a way to fix this using view_context or if there's a better way to include a link inside a Flash message.
You need to use
html_safewhen outputting the flash messages - not when storing them..html_safejust sets a flag on the string object that its trusted and should not be escaped.The flash works by storing flash messages in the session storage - by default this means a cookie in the browser.
So when you do:
You're storing the raw string "foo" in a cookie* and its unpacked back into the session on the next request. But the string is not the same Ruby object - so the
html_safeflag on the string object is not persistent.