Bootstrap 5.1 无法在 JSBundling Rails 7 中工作?
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交互工作)
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 & setuprake assets:clobber && ./bin/setup && yarn install && bundle install
then run ./bin/dev
Result: (no js interactions work)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在让 Bootstrap 5.1.3 javascript 在 Rails 7.0.2.4 中工作时遇到了一些重大问题。工具提示和下拉菜单是断断续续的。
首先始终使用 bin/dev 启动开发中的 Rails 服务器(而不是
rails 服务器
命令)。为了让 javascript 功能重新回到我的 Rails 7 应用程序中,我必须进入
app/javascript/application.js
并将其更改为此,
一切都开始按预期工作!
参考文献
您还可以尝试的操作
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 thisto this
and everything began to work as expected!
References
Things you may also try
从 Importmap 迁移到 JSBundling + CSSBundling 时,请确保删除残留文件。删除
config/importmap.rb
和bin/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 中工作:
请确保:
已添加到
app/javascript/application.js
文件中。查看
app/assets/stylesheets
是否有*.scss
(可能是application.bootstrap.scss
或application.scss
,只要package.json
中提供相同的名称就可以了),里面的内容是:在
app/assets/config/manifest.js中
文件,确保包含构建目录:不要忘记在
assets.rb
中包含资产文件。下面的评论中提到了如何设置相同的内容。When migrating from Importmap to JSBundling + CSSBundling, make sure that you remove the remnant files. Delete
config/importmap.rb
andbin/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 ofapp/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 usingrails javascript:install:[esbuild|rollup|webpack]
andrails 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:
has been added to the
app/javascript/application.js
file.See if
app/assets/stylesheets
has*.scss
(could beapplication.bootstrap.scss
orapplication.scss
, doesn't matter as long as the same name is provided inpackage.json
), and the content inside is:In the
app/assets/config/manifest.js
file, make sure that the builds directory is included:Don't forget to include assets in
assets.rb
file. Mentioned below in the comments on how to setup the same.确保您在 Bootstrap 5 中使用正确的标记。Bootstrap 4 标记将不起作用。具体来说,查找下拉元素的数据属性更改为:
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:
就我而言,它
导致了问题,因此对于我的 html 元素,我
通过此添加了我的下拉菜单,该下拉菜单在早些时候崩溃了,
即使在设置“data-turbo”= false之后,我观察到的另一件事仍然存在,因为需要确保您到达 current_page 的链接的目标属性设置为 self ie
in my case it was
causing the issue so for my html element I added
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