开发者

Ruby on Rails Helper Method - HTML is displayed as plain text

I have a helper method for my Rails app that returns a string with HTML code for a Google Groups subscription form. Unfortunately, it comes out on the page like plain text. How can I force 开发者_开发问答it to render as HTML? Thanks in advance.


The result of your helper needs to be marked as "html_safe" in Rails 3. Otherwise, the tags will be escaped.

def my_helper
  data = "<p>Hello!</p>"
  data.html_safe
end


I suppose it was a problem with Rails html sanitize. from rails changelog

You no longer need to call h(string) to escape HTML output, it is on by default in all view templates. If you want the unescaped string, call raw(string).

try it


One thing to watch out for is when joining multiple strings like this

def custom_check_box(checked)
    '<span class="my-custom-checkbox '+( checked ? 'checked' : '')+'"></span>'.html_safe
end

In this case, only the last part is marked as html safe. Make sure you get the whole thing.

def custom_check_box(checked)
    ('<span class="my-custom-checkbox '+( checked ? 'checked' : '')+'"></span>').html_safe
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜