开发者

Rail 3 - flash messages escaped, best way to unescape

开发者 https://www.devze.com 2023-01-17 14:22 出处:网络
Now that Rails 3 escapes all evaluated elements in views (good), but it is also escaping my flash message. And I like the occasional link in my flash message (or strong or emphasis).

Now that Rails 3 escapes all evaluated elements in views (good), but it is also escaping my flash message. And I like the occasional link in my flash message (or strong or emphasis).

A flash message:

flash[:notice] = "<strong>Bob开发者_如何转开发</strong> was sucessfully checked in.<br />If you made a mistake, you can <a href=\"/scouts/#{@scout.id}/check_out\">check out Bob</a> to undo."

Becomes garbled, escaped, unusable.

I can unescape the message using raw:

- flash.each do |name, msg|
  = content_tag :div, raw(msg)

But now every flash message is unescaped.

How can I unescape just a singe flash message? or just parts of the message?


try html_safe in your controller for each notice you want escaped. This will allow you to remove the raw function from the view and unescape only the ones you want in the controller.

flash[:notice] = "test<br/>test".html_safe
0

精彩评论

暂无评论...
验证码 换一张
取 消