gpt4 book ai didi

ruby-on-rails - 在 Rails 中使用 Bootstrap gem

转载 作者:行者123 更新时间:2023-12-01 03:12:22 25 4
gpt4 key购买 nike

我有一个新的 rails 项目,我只生成了一个 Controller 。我已按照 Bootstrap gem 的说明安装 Bootstrap ,但我不断收到以下错误:

ActionView::Template::Error (identifier '(function(opts, pluginOpts) {return eva
l(process' undefined):
5: <%= csrf_meta_tags %>
6: <%= csp_meta_tag %>
7:
8: <%= stylesheet_link_tag 'application', media: 'all', 'data-turbol
inks-track': 'reload' %>
9: <%= javascript_include_tag 'application', 'data-turbolinks-track': '
reload' %>
10: </head>
11:

(execjs):1
app/views/layouts/application.html.erb:8:in `_app_views_layouts_application_html
_erb__511219785_80461480'

我已按照此网页的所有说明进行操作: https://github.com/twbs/bootstrap-rubygem

我的代码:
# Gemfile
gem 'bootstrap', '~> 4.1.1'
gem 'jquery-rails'

# app/assets/javascripts/application.js
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap
//= require_tree .

# app/assets/stylesheets/application.scss
@import "bootstrap";

请注意,我也确实从 application.scss 中删除了 *= require 和 *= require_tree 并且我确实确保它是一个 scss 文件而不是一个 css 文件。

最佳答案

这似乎是 Windows 上 ExecJS 和 duktape 的当前问题。

有关详细信息,请参阅以下链接:https://github.com/twbs/bootstrap-rubygem/issues/157

简而言之,要解决此问题,您只需从 Gemfile 中删除/注释掉 duktape。如果您打算使用 Node.js 作为 JS 运行时,请记住实际安装它 (Node.js)。

如果仍有问题,请删除所有 //= require来自您的 application.scss 的指令并将其保留在 application.js 中。

应用程序.js

//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
//= require jquery3
//= require popper
//= require bootstrap-sprockets

应用程序.scss
@import "bootstrap";

gem 文件

删除 gem 'duktape'
gem 'autoprefixer-rails'
gem 'bootstrap', '~> 4.1.1'
gem 'jquery-rails'

从对该问题的评论:

duktape has become the default JS Runtime in Windows sometime ago in rails/rails#30014.

If you look into other similar issues involving Exejs you might found out that duktape is actually the culprit (however its execjs here for definition of duktape context). If you change your runtime environment to use Nodejs, the error would be resolved, as the case with @yasunari89

In config/boot.rb ENV['EXECJS_RUNTIME'] = 'Node'

You can find more info in #152 , #153 and #155 . However, above issues with invalid regex was fixed with new version of duktape, for more info visit judofyr/duktape.rb#41 The new version which resolved the invalid regex issue , started causing issue which you are encountering now. This issue happens because ( as defined in execjs) duktape doesn't support complex contexts and full JS as identifier.

However a PR is under review that will possibly resolve the issue, thanks to @judofyr



您还应该确保您的 application.html.erb设置正确。这是一个例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>title</title>

<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>
<div class="container">
<%= yield %>
</div>
</body>
</html>

ExecJS 支持多种运行时,而不仅仅是 duktape 和 NodeJS。
https://github.com/rails/execjs

ExecJS supports these runtimes:

therubyracer - Google V8 embedded within Ruby
therubyrhino - Mozilla Rhino embedded within JRuby
Duktape.rb - Duktape JavaScript interpreter
Node.js
Apple JavaScriptCore - Included with Mac OS X
Microsoft Windows Script Host (JScript)
Google V8
mini_racer - Google V8 embedded within Ruby

关于ruby-on-rails - 在 Rails 中使用 Bootstrap gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51105327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com