TabsOnRails graduated from beta

October 29th, 2009 at 9:00 pm • permalink8 comments

TabsOnRails 1.0.0 is now available. TabsOnRails is a simple Rails plugin for creating and managing tabs menu and navigation menu with Ruby on Rails. It provides helpers for creating tabs, an navigation menu in general, with a flexible interface.

The first stable release is now available and I’m also really proud to say that TabsOnRails has 100% coverage of the source code. This doesn’t mean the plugin is perfect, nor I can assume this is bug-free. However, the plugin definitely deserves the stable flag.

I have been using TabsOnRails for creating tabs and navigation menu in the last year and I’m really satisfied with how the plugin evolved. I hope you’ll enjoy it.

Getting Started

In your template use the tabs_tag helper to create your tab.

<% tabs_tag do |tab| %>
  <%= tab.home       'Homepage', root_path %>
  <%= tab.dashboard  'Dashboard', dashboard_path %>
  <%= tab.account    'Account', account_path %>
<% end %>

The example above produces the following HTML output.

<ul>
  <li><a href="/">Homepage</a></li>
  <li><a href="/dashboard">Dashboard</a></li>
  <li><a href="/account">Account</a></li>
</ul>

The usage is similar to the Rails route file. You create named tabs with the syntax tab.name_of_tab.

The name you use creating a tab is the same you’re going to refer to in your controller when you want to mark a tab as the current tab.

class DashboardController < ApplicationController
  set_tab :dashboard
end

Now, for every action defined in DashboardController, the template will automatically render the following HTML code.

<ul>
  <li><a href="/">Homepage</a></li>
  <li>Dashboard</li>
  <li><a href="/account">Account</a></li>
</ul>

Use the current_tab helper method if you need to access the value of current tab in your controller or template.

class DashboardController < ApplicationController
  set_tab :dashboard
end

# In your view
The name of current tab is <%= current_tab %>.

The open_tag can be customized with the :open_tabs option.

<% tabs_tag :open_tabs => { :id => "tabs", :class => "cool" } do |tab| %>
  <%= tab.home      'Homepage', root_path %>
  <%= tab.dashboard 'Dashboard', dashboard_path %>
  <%= tab.account   'Account', account_path %>
<% end %>

<ul id="tabs" class="cool">
  <li><a href="/">Homepage</a></li>
  <li><a href="/dashboard">Dashboard</a></li>
  <li><a href="/account">Account</a></li>
</ul>

Further customizations require a custom Builder (see below).

Installation

You can upgrade/install the library via RubyGems.

$ gem install tabs_on_rails --source http://gemcutter.org

Please note the GEM is now hosted on Gemcutter. If you installed it from GitHub you need to uninstall weppos-tabs_on_rails and install tabs_on_rails.

As usual, patches and feedback are welcomed.

  1. TabsOnRails: creating and managing Tabs with Ruby on Rails
  2. TabsOnRails 0.3.0 with concurrent tabs support aka namespaces
  3. TabsOnRails 0.8.0
  4. TabsOnRails and Helperful migrated to Gemcutter
  5. ActiveRecord::MultiConditions development discontinued

Filed in Programming • Tags: , , , ,


Comments

Ryan B says:

I am having some trouble trying to get the custom builder to work. Where do you recommend actually storing the custom builder in the rails project? I keep getting this error

uninitialized constant ActionView::Base::CompiledTemplates::MenuTabBuilder

when i try to feed the custom builder class to the tabs_tag.

If you declared the class inside an helper module, you need to pass the full qualified path.

tabs_tag :builder => PostHelpers::MyBuilder

Otherwise, you probably declared the class elsewhere and you forgot to include the library in your Rails environment.

I recommend to define the custom builder in the helper file. If it is a global builder, use the ApplicationHelper module, otherwise a specific helper is just fine.

The you can either pass the full qualified class path including namespaced as shown before, or you can creare a custom helper within the helper module and extend the tabs_tag.

module ApplicationHelper

  class MyBuilder
    # ...
  end

  def my_tabs_tag(options = {})
    tabs_tag(options.merge(:builder => MyBuilder))
  end

end
Ryan B says:

awesome thanks so much

Ryan B says:

Got the custom builder to work, but when I overwrite open_tabs or close_tabs I get


wrong number of arguments (1 for 0)

I am just mimicking the default

def open_tabs
''
end

accompanying backtrace

/opt/local/lib/ruby/gems/1.8/gems/tabs_on_rails-1.0.0/lib/tabs_on_rails/tabs.rb:32:in `open_tabs'
/opt/local/lib/ruby/gems/1.8/gems/tabs_on_rails-1.0.0/lib/tabs_on_rails/tabs.rb:32:in `send'
/opt/local/lib/ruby/gems/1.8/gems/tabs_on_rails-1.0.0/lib/tabs_on_rails/tabs.rb:32:in `open_tabs'
/opt/local/lib/ruby/gems/1.8/gems/tabs_on_rails-1.0.0/lib/tabs_on_rails/controller_mixin.rb:124:in `tabs_tag'
/Users/Ryan/Sites/Rails/perstrat/app/views/layouts/admin/projects.html.erb:28:in `_run_erb_app47views47layouts47admin47projects46html46erb'
/Users/Ryan/Sites/Rails/perstrat/app/controllers/admin/shops_controller.rb:8:in `index'

Ryan B says:

there should have been a ul tag in side the single quotes, but must have gotten stripped

def open_tabs(*args)
end

or

def open_tabs(options)
end

open_tabs requires one parameter in the method definition.

Christian says:

I was wondering if you have any recommendations for implementing dynamic loading with this? I am attempting to use it to load different tabs of text within a page. We have most of it working but it re loads the page when you click on a different tab. Would this be something we implement within a tabbuilder?

Thanks and grand work!

Christian,
what you need is a client-side JavaScript effect. Have a look at http://docs.jquery.com/UI/Tabs

Add a Comment




Follow Me
    Random Quote