Rails 6: How to add jquery-ui through webpacker?

后端 未结 8 946
失恋的感觉
失恋的感觉 2021-01-05 04:12

I\'m currently trying to implement a datepicker into my application, the problem is that there is no documentation on how to add the jquery-ui-rails gem through

8条回答
  •  难免孤独
    2021-01-05 04:39

    For me a hybrid of several articles, and things worked and looked the most elegant

    CLI:

        yarn add jquery jquery-ui-dist
    

    app/javascript/packs/application.js:

    // ... SNIP ...
    require("jquery")
    require("jquery-ui")
    

    config/webpack/environment.js:

    const { environment } = require('@rails/webpacker')
    
    const webpack = require('webpack')
    environment.plugins.prepend('Provide',
      new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery'
      })
    )
    
    const aliasConfig = {
        'jquery': 'jquery/src/jquery',
        'jquery-ui': 'jquery-ui-dist/jquery-ui.js'
    
    };
    
    environment.config.set('resolve.alias', aliasConfig);
    
    module.exports = environment
    

    app/views/layouts/application.html.erb

    <%= stylesheet_link_tag '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css' %>
    

提交回复
热议问题