开发者

how to use content_tag in a lib/class

my problem: I want to create a class TitlePanel in my lib folder that class uses the content_tag method but I can't figure out how to load it. I have tried all require 'xxx' that I could think of and it keeps giving me error messages that it can't find the required file.

Basically, what I am trying to do is create a helper that generates html, but I have to pass thru a class to store some value first. Ex of what I am trying to do:

title = TitlePanel.new("this is my title")
title.add_panel "help" do
  content_tag :div, "this is the help section..."
end
title.a开发者_运维知识库dd_panel "search" do
   content_tag :div, "this is the search section..."
end

title.to_s

the output being all the required HTML to make this work.


This is an old question, but I found this first and had the same trouble as OP. Best solution I found was to use this:

ActionController::Base.helpers.content_tag(:div, nil, class: 'my_div')


Give this a shot. If you include TagHelper in at the top of your file in your lib directory, it should work. Here's an example:

class MyLib
  include ActionView::Helpers::TagHelper

  def foo(x)
    content_tag :div, x
  end
end


>> MyLib.new.foo "bar"
=> "<div>bar</div>"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜