0

I want to use link_to tag inside a controller. I added include ActionView::Helpers::UrlHelper and here is the my code.

def authenticate_user!
    unless user_signed_in?
        flash[:danger] ="You must sign in or sign up first!#{link_to("test", root_path) }".html_safe
        redirect_to new_session_path
    end
end

If I go to the browser, it shows You must sign in or sign up first!<a href="/">test</a>

Do you know how can I show this message correctly? (I mean without tag)

Thank you!

  • 1
    Where do you render out the `flash[:danger]` message in your view or layout? – tadman Sep 19 '20 at 04:15
  • 1
    `flash` messages are stored in the session and read from it again when the user loads the next page. Therefore the `html_safe` has no effect in this case because when read from the session the string is considered unsafe again. – spickermann Sep 19 '20 at 06:06
  • 1
    on the view/layout where you render the `flash`, you need to call `raw flash[:danger]`. Read more https://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html – Thang Sep 19 '20 at 06:08
  • @tadman Yes, I pass this data to view and use content_tag to show this message. – user14035795 Sep 19 '20 at 21:33
  • @spickermann ok! But either way I have html_safe or not, I still can not show the message properly. I mean it shows with tag around the message. – user14035795 Sep 19 '20 at 21:36

0 Answers0