I have 4 tab sections in an Event page view. I have it working where ALL the content for each tab is fetched and rendered when an Event page is requested. This doesn't seem like a good solution for scalability...
How do I fetch the content for each tab using AJAX when a tab is clicked on?
(First time doing it... looks simple in theory but failing to get it working)
I tried adding remote: true
to the tab links but that doesn't play well with the in-page references - it makes the same get request Events#Show
for all the tabs which makes it difficult to determine which tab content is requested. If I change the tab href's to their respective controller links, then it breaks the bootstrap tab switching. I'm loosing either way.
Each tab's content is broken into separate controllers so individual content can be fetched.
For Tab#2, I need to fetch the content for which ever nav-pill
is active. And if I'm on Tab#2, I want to switch its content from Overview to Details and back using the nav-pills
button links.
Using Rails 4 w/ bootstrap 3, haml, coffeescript.
events/show.html.haml
/ Nav tabs %ul.nav.nav-tabs.color %li.active =link_to "Tab1", "#tab1", html_options = { "data-toggle" => "tab" } %li =link_to "Tab2", "#tab2", html_options = { "data-toggle" => "tab" } %li =link_to "Tab3", "#tab3", html_options = { "data-toggle" => "tab" } %li =link_to "Tab4", "#tab4", html_options = { "data-toggle" => "tab" } / Tab panes .tab-content .tab-pane.active#tab1 %p Fetch content .tab-pane.active#tab2 %ul.nav.nav-pills %li.active= link_to 'Overview', "#overview", html_options = { "data-toggle" => "tab"} %li= link_to 'Details', "#details", html_options = { "data-toggle" => "tab"} .tab-content .tab-pane.active#overview %p Fetch overview .tab-pane.active#details %p Fetch details .tab-pane.active#tab3 %p Fetch content .tab-pane.active#tab4 %p Fetch content
controllers/events_controller.rb
def show # currently have several database queries to get data for each tab :( respond_to do |format| format.html format.js end end
views/events/show.js.erb (This was for the remote:true
case but should be in the respective controller view for each tab)
$('div#tab2').append("<%= escape_javascript(render(partial: 'overview', locals: {names: @names, genders: @genders, ages: @ages})) %>")