0

次のようにhtmlマークアップを返すRuby gemを作成しようとしています:

class Hola
    def self.hi(name = "world")
        "hello #{name}"
    end

    def self.hi_with_markup(name = "world")
        "<strong>hello #{name}</strong>"
    end
end

ただし、次のように test.html.erb ファイルで使用しようとするたびに:

<%= Hola.hi_with_markup(", please work!") %>

実際に html をレンダリングする代わりに、タグを印刷した文字列を返します。gem 側からこれを修正するにはどうすればよいですか?

ありがとう!

4

2 に答える 2

2

Rails 3 では、安全でないと見なされるすべての文字列について、HTML を「エスケープしない」から HTML をエスケープする (つまり、'>' のようなものを > に変換する) にデフォルトが変更されました。これは通常、gem の出力を含む、ユーザー文字を持つ可能性のある任意の文字列です。これには 2 つの方法がありraw()ます.html_safe

ここに包括的な答えがあります: raw vs. html_safe vs. h to unescape html

短い答えはこれを行うことです:

<%= Hola.hi_with_markup(", please work!").html_safe %>

また

<%= raw(Hola.hi_with_markup(", please work!")) %>
于 2013-01-16T18:06:10.400 に答える
1

これを試して:

class Hola
    def self.hi(name = "world")
        "hello #{name}"
    end

    def self.hi_with_markup(name = "world")
        "<strong>hello #{name}</strong>".to_html
    end
end
于 2013-01-16T17:57:59.770 に答える