Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 507
Alain Avatar asked Jun 04 '10 14:06

Alain


2 Answers

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>"
like image 163
Jim Jones Avatar answered Sep 18 '22 18:09

Jim Jones


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')
like image 22
d3vkit Avatar answered Sep 22 '22 18:09

d3vkit