LoginSignup
7
4

More than 3 years have passed since last update.

content_tagの使い方について

Posted at

背景

見たこと無い記述が出てきて調べた際に便利だったので、備忘録として書きます。

content_tagとは

Railsのビューヘルパーで、これを用いるとHTMLコードの要素を生成することが出来ます。
これを使うことでスマートなコードを書くことができます!

使い方


#通常
content_tag(:p)
#=> "<p></p>"

content_tag(:p, "テスト", id: "test", class: "test-class")
#=> "<p id="test" class="test-class">テスト</p>"

# 入れ子
content_tag(:div, id: "hoge") do
 content_tag(:p, "テスト")
end
#=> "<div id="hoge"><p>テスト</p></div>"

# ヘルパーに切り出して使う
def helper
 #処理
 hoge = 
 content_tag(:div, hoge) 返り値
end
7
4
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
7
4