ruby-on-rails - rails Assets - 单击链接时无法正常工作,但在地址栏中刷新或按下 Enter 时工作。提供了 Heroku 链接

标签 ruby-on-rails

这是我在 heroku 上的应用程序的链接:http://glacial-temple-5892.herokuapp.com/

它有一个包含 4 个链接的下拉菜单,所有链接都指向主页 url。

如果您先单击“下拉列表”,则下拉列表将打开并显示 4 个链接。当一个链接被点击时,它会来到同一个页面(带有路由'/'的主页),然后点击下拉菜单现在不起作用。但是,当我们在地址栏上刷新或按 Enter 键时,它会起作用。

解决办法是什么?

应用程序.html.erb

<!DOCTYPE html>
<html>
<head>
    <title>Assets test</title>
<%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
    <a class="navbar-brand" href="#">Hello </a>
    <ul class='nav navbar-nav'>
        <li><a >link</a></li>
        <li><a >link</a></li>
        <li class='dropdown'>
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href='/'>link 1</a></li>
                    <li><a href='/'>link 2</a></li>
                    <li><a href='/'>link 3</a></li>
                    <li><a href='/'>link 4</a></li>
                </ul>
        </li>
        </ul>
</div>
</nav>
    <p class='notice'><%= notice %></p>
    <p class='alert'><%= alert %></p>
<%= yield %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
</body>
</html>

应用程序/ Assets /javascripts/application.js
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives)        for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//=require dropdown
//= require_tree .

应用程序/ Assets /样式表/application.css
/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the top of the
 * compiled file, but it's generally better to create a new file per style scope.
 *
 *= require_self
 *= require_tree .
 *=require bootstrap
 */

我将 bootstrap.css 放在供应商/样式表中
我将 dropdown.js 放在 vendor/javascripts/目录中

最佳答案

检查这个 topic :由于 turbolinks 不会触发 Document.ready 事件而导致的相同问题。

关于ruby-on-rails - rails Assets - 单击链接时无法正常工作,但在地址栏中刷新或按下 Enter 时工作。提供了 Heroku 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18956989/

相关文章:

ruby-on-rails - 仅覆盖默认范围(特别是订单),而不覆盖 Rails 中的其他任何内容

ruby-on-rails - Rails 完整错误页面未显示 View 错误

ruby-on-rails - 在Ruby on Rails中为每个用户分配一个唯一的100个字符的哈希

javascript - 为什么我有多个 &lt;script&gt; 标签而不是 Rails 应用程序中的一个?

ruby-on-rails - 如何使用嵌套属性查找模型

ruby-on-rails - Rails 4 ActiveRecord::关联类型不匹配

javascript - 将 JSON 从索引操作提供给公共(public)文件夹中的 index.html

ruby-on-rails - ubuntu 用户和 ssh 登录权限被拒绝消息

ruby-on-rails - rails activerecord 字段的数据库表达式

ruby-on-rails - Hartl 教程中的 bundle exec rspec spec/requests/static_pages_spec.rb 不起作用