【问题标题】:Rails: redirect_to specific Twitter Bootstrap tabRails:redirect_to 特定的 Twitter Bootstrap 选项卡
【发布时间】:2013-08-22 01:00:07
【问题描述】:

如何将重定向更改为特定页面上的特定 Twitter Bootstrap 选项卡?比如:

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

谢谢!

更新

查看@PeterWong 和@emm 的答案组合以获得解决方案。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 twitter-bootstrap


    【解决方案1】:

    这点 JS 帮我修好了:

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

    它对 turbolinks 做了一些奇怪的事情,但由于这都是基于重定向的,所以这不是问题。

    【讨论】:

      【解决方案2】:

      在您的控制器中:

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

      文档控制器:

      class DocumentsController < ApplicationController
      
        def index
          @tab = params[:tab]
        end
      end
      

      在html中:

      <div class="tab-pane <%= @tab == "specific_tab" ? "active" : "" %>">
      

      【讨论】:

        【解决方案3】:

        这个怎么样?

        redirect_to "#{documents_url}#specific_tab"
        

        【讨论】:

        • 好主意,但没有骰子:它转到 URL 栏中的正确位置,但选项卡不对应。
        • 附加了井号标签的 url 是否完全正确?如果是这样,它可能是一个javascript问题而不是rails。你设置正确了吗?
        • 是的,确实如此,是的,它设置正确。这可能只是一个JS问题吧?也许默认值会覆盖重定向。
        • 在关闭默认设置后,控制台会给我这个: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.
        • 是不是直接把url和hash标签一起输入到浏览器,tab也不会切换?如果是这样,那么 rails 部分是固定的,现在问题出现在纯 javascript 方面。可能需要更多信息,例如您的 html / js 代码的结构等。建议在此处发布更详细的信息,或者只是创建另一个有关 javascript 的问题,以便更多特定于 js 的开发人员可以提供帮助。
        【解决方案4】:

        使用代码对 @emm 解决方案进行小调整,以使用 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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-02
          • 1970-01-01
          • 2013-08-27
          • 2012-08-21
          • 1970-01-01
          相关资源
          最近更新 更多