I’ve been working on a project using beta 3 of Rails3, and was looking to use tabs from the jQuery UI project. Having run across a post featuring a TabsRenderer helper class, I thought I’d give it a whirl. The code from that post was written in 2008, and it did not work with the beta of Rails3, so I set out to see if I could get it working.
The original code for the helper class, when executed in Rails3, was rendering escaped HTML for the tab bodies, and nothing for the tab headers. Simply changing the call in the view to tabs.render to this:
tabs.render.html_safe
didn’t do the trick for unescaping the tab bodies. What ended up working was using this for the TabsRenderer class:
class TabsRenderer
def initialize(template, options={})
@template = template
@options = options
@tabs = []
end
def create(tab_id, tab_text, options={}, &block)
raise "Block needed for TabsRenderer#CREATE" unless block_given?
@tabs << [tab_id, tab_text, options, block]
end
def render
content_tag(:div, (render_tabs.html_safe + render_bodies.html_safe), {:id => :tabs}.merge(@options))
end
private # ---------------------------------------------------------------------------
def render_tabs
content_tag(:ul, render_headers.html_safe)
end
def render_headers
@tabs.collect do |tab|
content_tag(:li, link_to(content_tag(:span, tab[1]), "##{tab[0]}") )
end.to_s
end
def render_bodies
@tabs.collect do |tab|
content_tag(:div, capture(&tab[3]), tab[2].merge(:id => tab[0]))
end.to_s
end
def method_missing(*args, &block)
@template.send(*args, &block)
end
end
Everything else about the sample code on the original post can be used as originally presented.
Hope this helps you as you adjust to Rails3!
{ 2 comments… read them below or add one }
Thanks for taking the time to post this solution. I was banging my head and cursing and now I’m smiling and happy. The original code offered a nice way to use jQuery UI tabs and I’m happy to see it it working in Rails 3.
Glad it was of help to you. Sorry for being so tardy in moderating comments - wish I could just leave them open but the spam-to-comment ratio would be off the charts.