やりたい事
Railsのdraperのdecoratorで、スマートにHTMLタグを生成したい。
今回は例えで"<span>hello</span>"
をば。
"<span>hello</span>".html_safe
でもできるけど、tagヘルパーを使ってもうちょいスマートに。
- tagヘルパー参考サイト
実装!
単にこう書いただけではエラーになる。
app/decorators/hello_decorator.rb
>> tag.span('hello')
=> undefined local variable or method 'tag' for #<HelloDecorator:0x00000000000> Did you mean? tap
答え1
app/decorators/hello_decorator.rb
>> ApplicationDecorator.helpers.tag.span("hello")
=> "<span>hello</span>"
答え2
Draperのドキュメントをちゃんと読むと、もっと簡単に書けるらしい。
https://github.com/drapergem/draper#accessing-helpers
>> h.tag.span("hello")
=> "<span>hello</span>"
毎度ありがとうございます、 @Y_uuu さん。