Skip to content

Instantly share code, notes, and snippets.

@MiguelSavignano
Last active November 17, 2016 23:45
Show Gist options
  • Save MiguelSavignano/9aad18bff359389ead309ac722ee05c0 to your computer and use it in GitHub Desktop.
Save MiguelSavignano/9aad18bff359389ead309ac722ee05c0 to your computer and use it in GitHub Desktop.
Tabs rails
def link_tab(content, tab_name, options={})
prefix_active = "active"
tab_name = tab_name.to_s
css_class =
if params[:tab].blank?
options[:default] ? prefix_active : nil
elsif params[:tab] == tab_name
prefix_active
end
link_to content, current_path({tab: tab_name}), class: css_class
end
def tab_view(tab_name, options={})
tab_name = tab_name.to_s
show_tab = params[:tab].blank? ? options[:default] : params[:tab] == tab_name
if show_tab
yield
else
nil
end
end
<%= link_tab "Tab1", :tab1, default: true %>
<%= link_tab "Tab2", :tab2 %>
<%= tab_view(:tab1, default:true) do %>
<%= render 'tab1' %>
<% end %>
<%= tab_view(:tab2) do %>
<%= render 'tab2' %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment