3

How would I change a redirect to a specific Twitter Bootstrap tab on a specific page? Something like:

def destroy
  @custom_article = CustomArticle.find(params[:id])
  @custom_article.destroy
  respond_to do |format|
    format.html { redirect_to documents_url[#specific_tab_here?] }
    format.json { head :no_content }
  end
end

Thanks!

UPDATE

See @PeterWong's and @emm's answers combined for a solution.

t56k
  • 6,769
  • 9
  • 52
  • 115

4 Answers4

6

This bit of JS fixed it up for me:

$(function(){
  var hash = window.location.hash;
  hash && $('ul.nav a[href="' + hash + '"]').tab('show');
});

It does something a little strange with turbolinks but since this is all redirect-based it's not an issue.

t56k
  • 6,769
  • 9
  • 52
  • 115
4

In your controller:

def destroy
  redirect_to documents_url(tab: "specific_tab")
end

DocumentsController:

class DocumentsController < ApplicationController

  def index
    @tab = params[:tab]
  end
end

In the html:

<div class="tab-pane <%= @tab == "specific_tab" ? "active" : "" %>">
carpamon
  • 6,515
  • 3
  • 38
  • 51
1

How about this one?

redirect_to "#{documents_url}#specific_tab"
PeterWong
  • 15,951
  • 9
  • 59
  • 68
  • 1
    Good idea, but no dice: it goes to the right place in the URL bar but the tabs don't correspond. – t56k Aug 22 '13 at 02:44
  • Does the url completely correct with the hash tag appended? If so, it might be a javascript issue instead of rails. Did you setup correctly? – PeterWong Aug 22 '13 at 03:31
  • Yeah, it does, and yeah, it's set up correctly. It could only be a JS issue right? Maybe the defaults are overriding the redirect. – t56k Aug 22 '13 at 03:40
  • Console gives me this once defaults are turned off: `Given URL is not permitted by the application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains. ` – t56k Aug 22 '13 at 03:51
  • is it a facebook app? – PeterWong Aug 22 '13 at 03:57
  • It has the ability to post to Facebook, that's all. – t56k Aug 22 '13 at 04:04
  • 1
    Is it true that even you enter the url together with the hash tag directly to the browser, the tab doesn't switch as well? If so, then the rails part is fixed and now the issue goes to pure javascript side. Might need more information such as how your html / js code structured etc. Would suggest either posting more detailed info here, or just create another questions about javascript so that more js-specific developers could help. – PeterWong Aug 22 '13 at 04:08
  • Yeah, the tab doesn't switch even when typed directly into the browser. Will investigate. Thanks. – t56k Aug 22 '13 at 04:13
1

A small tweak to @emm solution with the code to get to work with turbolinks:

var ready;

ready = function() {
  var hash = window.location.hash;
  hash && $('ul.nav a[href="' + hash + '"]').tab('show');
};

$(document).ready(ready);
$(document).on('page:load', ready);
Steve
  • 2,596
  • 26
  • 33