Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tabs_on_rails

> 编程语言
Open source

Tabs on Rails is a simple Rails plugin for creating and managing tabs and navigation menus.

297 stars0 likes1 views
WebsiteGitHub

About

Tabs on Rails is a simple Rails plugin for creating and managing tabs and navigation menus.

Tabs on Rails

TabsOnRails is a simple Rails plugin for creating tabs and navigation menus. It provides helpers for generating navigation menus with a flexible interface.

Requirements

  • Rails 4.2 or Rails 5

For older versions of Ruby or Ruby on Rails, see the CHANGELOG.

Installation

Add this line to your application's Gemfile:

gem "tabs_on_rails"

And then execute bundle to install the dependencies:

$ bundle

Use Bundler and the :git option if you want to grab the latest version from the Git repository.

Usage

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

ruby

  
  
  

renders

xml

  
Homepage

  
Dashboard

  
Account

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.

ruby
class DashboardController .

Customizing a Tab

You can pass a hash of options to customize the style and the behavior of the tab item. Behind the scenes, each time you create a tab, the #tab_for method is invoked.

ruby

   "padding: 10px" %>
  

renders


  
Homepage

  
Dashboard

  
Account

See TabsOnRails::Tabs::TabsBuilder#tab_for for more details.

Customizing open_tabs and close_tabs

The open_tabs and the close_tabs methods can be customized with the :open_tabs and :close_tabs option.

ruby

  
  
  

renders

xml

  
Homepage

  
Dashboard

  
Account

Further customizations require a custom Builder (see below).

Restricting set_tab scope

The set_tab method understands all options you are used to pass to a Rails controller filter. In fact, behind the scenes this method uses a before_filter to store the tab in the @tab_stack variable.

Taking advantage of Rails filter options, you can restrict a tab to a selected group of actions in the same controller.

ruby
class PostsController  %w(index show)
end

class ApplicationController  :admin_controller?
  
  def admin_controller?
    self.class.name =~ /^Admin(::|Controller)/
  end
end

Using Namespaces to create Multiple Tabs

Namespaces enable you to create and manage tabs in parallels. The best way to demonstrate namespace usage is with an example.

Let's assume your application provides a first level navigation menu with 3 elements: :home, :dashboard, :projects. The relationship between your tabs and your controllers is 1:1 so you should end up with the following source code.

ruby
class HomeController
  set_tab :home
end

class DashboardController
  set_tab :dashboard
end

class ProjectsController
  set_tab :projects
  
  def first; end
  def second; end
  def third; end
end

The project controller contains 3 actions and you might want to create a second-level navigation menu. This menu should reflect the navigation status of the user in the project page.

Without namespaces, you wouldn't be able to accomplish this task because you already set the current tab value to :projects. You need to create a parallel navigation menu and uniquely identify it with a custom namespace. Let's call it :navigation.

ruby
class ProjectsController
  set_tab :projects

  # Create an other tab navigation level
  set_tab :first, :navigation, :only => %w(first)
  set_tab :second, :navigation, :only => %w(second)
  set_tab :third, :navigation, :only => %w(third)

  def first; end
  def second; end
  def third; end
end

Voilà! That's all you need to do. And you can create an unlimited number of namespaces as long as you use an unique name to identify them.

The default namespace is called :default. Passing :default as name is the same as don't using any namespace at all. The following lines are equivalent.

ruby
set_tab :projects
set_tab :projects, :default

Rendering Tabs with Namespaces

To switch namespace in your template, just pass the :namespace option to the tabs_tag helper method.

ruby

  
  
  

 :navigation do |tab| %>
  
  
  

Namespace scope

As a bonus feature, the namespace needs to be unique within current request scope, not necessarily across the entire application.

Back to the previous example, you can reuse the same namespace in the other controllers. In this way, you can reuse your templates as well.

ruby
class HomeController
  set_tab :home
end

class DashboardController
  set_tab :dashboard

  set_tab :index,  :navigation, :only => %w(index)
  set_tab :common, :navigation, :only => %w(foo bar)
  
  # ...
end

class ProjectsController
  set_tab :projects

  set_tab :first,  :navigation, :only => %w(first)
  set_tab :second, :navigation, :only => %w(second)
  set_tab :third,  :navigation, :only => %w(third)
  
  # ...
end

Tab Builders

The Builder is responsible for creating the tabs HTML code. This library is bundled with two Builders:

  • Tabs::Builder: this is the abstract interface for any custom builder.
  • Tabs::TabsBuilder: this is the default builder.

Understanding the Builder

Builders act as formatters. A Builder encapsulates all the logic behind the tab creation including the code required to toggle tabs status.

When the tabs_tag helper is called, it creates a new Tabs instance with selected Builder. If you don't provide a custom builder, then Tabs::TabsBuilder is used by default.

Creating a custom Builder

All builders must extend the base Tabs::Builder class and implement at least the tab_for method. Additional overridable methods include:

  • open_tabs: the method called before the tab set
  • close_tabs: the method called after the tab set
  • tab_for: the method called to create a single tab item

The following example creates a custom tab builder called MenuTabBuilder.

ruby
class MenuTabBuilder  MenuTabBuilder) do |tab| %>
  
  
  

renders

xml

  
Homepage

  
Dashboard

  
Account

Credits

TabsOnRails was created and is maintained by Simone Carletti. Many improvements and bugfixes were contributed by the open source community.

Contributing

Direct questions and discussions to Stack Overflow.

Pull requests are very welcome! Please include tests for every patch, and create a topic branch for every separate change you make.

Report issues or feature requests to GitHub Issues.

More Information

  • Homepage
  • RubyGems
  • Issues

License

Copyright (c) 2009-2017 Simone Carletti. This is Free Software distributed under the MIT license.

Issues· 2 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言