简体   繁体   English

如何在 RoR 中正确安装 select2(或 Chosen - 或者任何 JS?)?

[英]How to properly install select2 (or Chosen - or maybe ANY JS?) in RoR?

Rails beginner here: I have played around with Chosen and saw some posts that Select2 is the better option. Rails 初学者:我玩过 Chosen,看到一些帖子说 Select2 是更好的选择。 However regardless of which one I am trying to use, I always run into the same error.但是,无论我尝试使用哪一个,我总是遇到同样的错误。

I have one of them in the Gemfile and run bundle install, and the gem is then present (I can run bundle info on it).我在 Gemfile 中有一个并运行 bundle install,然后 gem 就出现了(我可以在上面运行 bundle info)。

I then try to include the JS into app/javascript/packs/application.js like so:然后我尝试将 JS 包含到 app/javascript/packs/application.js 中,如下所示:

require ("select2")

or或者

require ("chosen-jquery")

Whatever I do - my JS console says无论我做什么 - 我的 JS 控制台说

Cannot find module 'select2'

(or chosen-jquery). (或选择的jquery)。

I am sure this is a super stupid oversight.我相信这是一个超级愚蠢的疏忽。 I googled like crazy and searched here but could not find anything (which probably means even more that this is a stupid oversicht: :)我疯狂地用谷歌搜索并在这里搜索,但找不到任何东西(这可能意味着更多的是这是一个愚蠢的 oversicht::)

Any hint is highly welcomed.任何提示都非常受欢迎。 I probably don't understand how all this webpacker / JS stuff is working.我可能不明白所有这些 webpacker / JS 的东西是如何工作的。 Note btw that I WAS able to get easy-autocomplete (and jquery) via YARN and then I CAN actually require easy-autocomplete in application.js (and also the stylesheets in application.scss) via:请注意,顺便说一句,我能够通过 YARN 轻松自动完成(和 jquery),然后我实际上可以通过以下方式在 application.js(以及 application.scss 中的样式表)中要求轻松自动完成:

require("easy-autocomplete")

I don't quite get why this one is working and the other ones not?我不太明白为什么这个有效而其他无效?

Here is my full app/javascript/packs/application.js (including autocomplete):这是我的完整 app/javascript/packs/application.js(包括自动完成):

import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"

Rails.start()
Turbolinks.start()
ActiveStorage.start()

require("jquery")
require("easy-autocomplete")

require("select2/dist/css/select2")


document.addEventListener("turbolinks:load", function() {
    var $input = $("[data-behaviour='autocomplete']")

    var options = {
        getValue: "name",
        url: function(phrase) {
            return "/search.json?q=" + phrase;
        },
        categories: [
            {
                listLocation: "movies",
                header: "<strong>Movies</strong>",
            },
            {
                listLocation: "actors",
                header: "<strong>Actors</strong>",
            },
            {
                listLocation: "actoraliases",
                header: "<strong>Actor Aliases</strong>",
            }
        ],
        list: {
            onChooseEvent: function() {
                var url = $input.getSelectedItemData().url
                $input.val("")
                Turbolinks.visit(url)
            }
        }
    }

    $input.easyAutocomplete(options)
})

document.addEventListener("turbolinks:load", function() {
    $('.selector').select2();
})

Thank you!谢谢!

If you're using select-2-rails this is only for the Rails Asset Pipeline.如果您使用的是select-2-rails ,这仅适用于 Rails 资产管道。

Try installing select2 via yarn (opposed to the ruby gem):尝试通过 yarn 安装select2 (与 ruby gem 相对):

yarn install select2

In app/javascript/packs/application.js :app/javascript/packs/application.js

require("select2/dist/css/select2")

$(".content-search").select2();

// Side Note: when I moved this to a Stimulus Controller `import Select2 from "select2"` was needed.

In app/views/foos/_form.html.erb :app/views/foos/_form.html.erb

<%= form.label :foo_id %>
<%= form.collection_select(:foo_id, Foo.all, :id, :name, { include_blank: true }, class: "content-search") %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM