Bootstrap 5.1 无法在 JSBundling Rails 7 中工作?

发布于 2025-01-09 05:58:06 字数 3168 浏览 1 评论 0原文

2023-01-09 – 已解决

我开始使用默认的 Rails 模板,现在我想将 Bootstrap v5.1 与 Rails 一起使用。我已删除 Importmap-Rails 并继续添加 JSBundling-Rails 和 CSSBundling-Rails。样式工作正常,但 Bootstrap JavaScript 命令似乎都不起作用。我做错了什么?

来自 rails new 的复制

rails new Xyz --css=bootstrap --javascript=esbuild && cd Xyz/ && ./bin/setup && git add . && git commit -m "initial commit"
rails generate controller Articles &&
echo "hello world" > app/views/articles/index.erb &&
sed -i '' -e  's/# root "articles#index"/root "articles#index"/g' config/routes.rb && 
git add . && git commit -m "generates Articles controller"

将默认引导程序导航添加到此文件中: 2023-01-09 – 已解决----这是旧的 Bootstrap 4 标记,这就是为什么它对我不起作用

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

设置 rake 资产:clobber && ./bin/setup &&纱线安装&&捆绑安装

然后运行./bin/dev

结果:(没有js交互工作)

在此处输入图像描述

预期结果:(JS 交互有效) 输入图片此处描述

2023-01-09 – SOLVED

I started using the default Rails template, and now I want to use Bootstrap v5.1 with Rails. I've removed Importmap-Rails and proceeded with adding JSBundling-Rails and CSSBundling-Rails. The styling works properly, but none of the Bootstrap JavaScript commands seem to be working. What am I doing wrong?

Reproduction from rails new

rails new Xyz --css=bootstrap --javascript=esbuild && cd Xyz/ && ./bin/setup && git add . && git commit -m "initial commit"
rails generate controller Articles &&
echo "hello world" > app/views/articles/index.erb &&
sed -i '' -e  's/# root "articles#index"/root "articles#index"/g' config/routes.rb && 
git add . && git commit -m "generates Articles controller"

add to this file the default bootstrap nav:
2023-01-09 – SOLVED—-- this is the old Bootstrap 4 markup which is why it wasn't working for me

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

clobber & setup
rake assets:clobber && ./bin/setup && yarn install && bundle install

then run ./bin/dev

Result: (no js interactions work)

enter image description here

Expected Result: (JS interactions work)
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2025-01-16 05:58:06

我在让 Bootstrap 5.1.3 javascript 在 Rails 7.0.2.4 中工作时遇到了一些重大问题。工具提示和下拉菜单是断断续续的。

首先始终使用 bin/dev 启动开发中的 Rails 服务器(而不是rails 服务器 命令)。

为了让 javascript 功能重新回到我的 Rails 7 应用程序中,我必须进入 app/javascript/application.js 并将其更改为此

import "@hotwired/turbo-rails"

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

一切都开始按预期工作!

参考文献


您还可以尝试的操作

  • 每次启动服务器之前,请使用以下命令清除所有内容:
rake assets:clean
rake assets:clobber
rake tmp:clear
  • 硬刷新ctrl + 单击刷新),或command + shift + r

I had some major problems getting bootstrap 5.1.3 javascripts working in rails 7.0.2.4. Tooltips and dropdowns were intermittent.

Firstly always use bin/dev to start the rails server in development (rather than the rails server command).

What I had to do to get javascript functionality back into my rails 7 app was go into app/javascript/application.js and change this

import "@hotwired/turbo-rails"

to this

import { Turbo } from "@hotwired/turbo-rails"
Turbo.session.drive = false

and everything began to work as expected!

References


Things you may also try

  • Each time before starting the server, use these commands to clear everything:
rake assets:clean
rake assets:clobber
rake tmp:clear
  • Hard refresh (ctrl + click on refresh) on every page load, or command + shift + r
逐鹿 2025-01-16 05:58:06

从 Importmap 迁移到 JSBundling + CSSBundling 时,请确保删除残留文件。删除config/importmap.rbbin/importmap

还会有一些包含 Hotwire Turbo 和 Hotwire Stimulus 的残余物。如果您不打算使用它们,请删除 app/javascript/controllers 并清除 app/javascript/application.js 内的行。如果万一您需要 hotwire 库,请首先将 jsbundling-rails 和 cssbundling-rails 添加到 Gemfile 中。首先使用 rails javascript:install:[esbuild|rollup|webpack]rails css:install:bootstrap 完成设置。

bin/dev 中可能存在一些冲突,但我没有注意到任何不同。因此,您可以丢弃或保留文件的冲突版本。

大多数必需的文件将自动生成,并且行会自动附加到预先存在的文件中,但如果 Bootstrap v5.1 仍然无法在使用 JSBundling 和 CSSBundling 的 Rails 中工作:

  • 请确保:

    从“bootstrap”导入*作为引导程序
    

    已添加到 app/javascript/application.js 文件中。

  • 查看app/assets/stylesheets是否有*.scss(可能是application.bootstrap.scssapplication.scss,只要package.json中提供相同的名称就可以了),里面的内容是:

    @import 'bootstrap/scss/bootstrap';
    @import 'bootstrap-icons/font/bootstrap-icons';
    
  • app/assets/config/manifest.js中 文件,确保包含构建目录:

    //= link_tree ../images
    //= link_tree ../../javascript .js
    //= link_tree ../../../vendor/javascript .js
    
    // 添加下面的行,其中包含双斜杠和等号,后跟空格。
    //= link_tree ../builds
    
  • 不要忘记在 assets.rb 中包含资产文件。下面的评论中提到了如何设置相同的内容。

When migrating from Importmap to JSBundling + CSSBundling, make sure that you remove the remnant files. Delete config/importmap.rb and bin/importmap.

There will also be some remnants containing Hotwire Turbo and Hotwire Stimulus. If you're not planning to use them, delete app/javascript/controllers and clear the lines inside of app/javascript/application.js. If in case, you want the hotwire libraries, first add jsbundling-rails and cssbundling-rails to the Gemfile. Complete their setup first using rails javascript:install:[esbuild|rollup|webpack] and rails css:install:bootstrap

There might be some conflict in bin/dev, but I haven't noticed anything different. So you can either discard, or keep the conflicted version of the file.

Most of the required files will be auto-generated, and lines auto-appended in pre-existing files, but if Bootstrap v5.1 still isn't working in rails, with JSBundling and CSSBundling:

  • Make sure that:

    import * as bootstrap from "bootstrap"
    

    has been added to the app/javascript/application.js file.

  • See if app/assets/stylesheets has *.scss (could be application.bootstrap.scss or application.scss, doesn't matter as long as the same name is provided in package.json), and the content inside is:

    @import 'bootstrap/scss/bootstrap';
    @import 'bootstrap-icons/font/bootstrap-icons';
    
  • In the app/assets/config/manifest.js file, make sure that the builds directory is included:

    //= link_tree ../images
    //= link_tree ../../javascript .js
    //= link_tree ../../../vendor/javascript .js
    
    // Add the line below, with the double slashes and equal sign, followed by spaces.
    //= link_tree ../builds
    
  • Don't forget to include assets in assets.rb file. Mentioned below in the comments on how to setup the same.

羁绊已千年 2025-01-16 05:58:06

确保您在 Bootstrap 5 中使用正确的标记。Bootstrap 4 标记将不起作用。具体来说,查找下拉元素的数据属性更改为:

:data => { "bs-toggle" => "dropdown"}

Be sure you are using the right markup with Bootstrap 5. The Bootstrap 4 markup won't work. Specifically, the data attribute to find dropdown elements was changed to:

:data => { "bs-toggle" => "dropdown"}
薄暮涼年 2025-01-16 05:58:06

就我而言,它

import "@hotwired/turbo-rails"

导致了问题,因此对于我的 html 元素,我

data-turbo="false" 
ie
<li class="nav-item dropdown" data-turbo="false">

通过此添加了我的下拉菜单,该下拉菜单在早些时候崩溃了,

即使在设置“data-turbo”= false之后,我观察到的另一件事仍然存在,因为需要确保您到达 current_page 的链接的目标属性设置为 self ie

target="_self"

in my case it was

import "@hotwired/turbo-rails"

causing the issue so for my html element I added

data-turbo="false" 
ie
<li class="nav-item dropdown" data-turbo="false">

by this my drop-down worked which was collapsing earlier

another thing I observed even after setting "data-turbo"=false some time problem persist for that need to make sure the link by which you arrived on current_page has target-attribute set to self ie

target="_self"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文