Alain
Alain

Reputation: 1251

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.add_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.

Upvotes: 15

Views: 7653

Answers (2)

d3vkit
d3vkit

Reputation: 1982

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')

Upvotes: 16

Jim Jones
Jim Jones

Reputation: 2837

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>"

Upvotes: 12

Related Questions