1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

html_safeの意味

Posted at

背景

controller.rbにhtmlを埋め込んでもhtmlの下のようにソースコードが表示されてしまう

失敗例
image.png

結論

render html: msg.html_safe
このように、html_safeを入れること
成功例
image.png

全体ソースコード

改善後sample
class HelloController < ApplicationController
    def index
        #render plain:"Hello, This is Rails sample page!"

        msg = '
        <html>
        <body>
            <h1>Sample Page</h1>
            <p>this is sample page!</p>
        </body>
        </html>
        '
        render html: msg.html_safe
    end
end

原因

Railsのrenderは基本的に、htmlのタグをすべてエスケープするみたい。
その理由としては、クロスサイトスクリプト攻撃のような外部から悪意のあるコードを受け取っても実行しないようにするためと思われる。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?