質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

5548閲覧

Rails6でWebpacker開発環境でRaty.jsを使いたい。

ytommy

総合スコア11

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/04/14 04:58

編集2020/04/14 05:00

前提・実現したいこと

Rails6でWebpacker開発環境でRaty.jsを使いたい。

(環境)
・ruby 2.6.5
・Rails 6.0.2.2
・Windows 10

発生している問題

jquery.raty.jsで5段階評価機能を実装したいが、jsが効いていない。

該当のソースコード

#webpacker.yml # Note: You must restart bin/webpack-dev-server for changes to take effect default: &default source_path: app/javascript source_entry_path: packs public_root_path: public public_output_path: packs cache_path: tmp/cache/webpacker check_yarn_integrity: false webpack_compile_output: true # Additional paths webpack should lookup modules # ['app/assets', 'engine/foo/app/assets'] resolved_paths: ['app/assets'] # Reload manifest.json on all requests so we reload latest compiled packs cache_manifest: false # Extract and emit a css file extract_css: false static_assets_extensions: - .jpg - .jpeg - .png - .gif - .tiff - .ico - .svg - .eot - .otf - .ttf - .woff - .woff2 extensions: - .mjs - .js - .sass - .scss - .css - .module.sass - .module.scss - .module.css - .png - .svg - .gif - .jpeg - .jpg development: <<: *default compile: true # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules check_yarn_integrity: true # Reference: https://webpack.js.org/configuration/dev-server/ dev_server: https: false host: localhost port: 3035 public: localhost:3035 hmr: false # Inline should be set to true if using HMR inline: true overlay: true compress: true disable_host_check: true use_local_ip: false quiet: false pretty: false headers: 'Access-Control-Allow-Origin': '*' watch_options: ignored: '**/node_modules/**' test: <<: *default compile: true # Compile test packs to a separate directory public_output_path: packs-test production: <<: *default # Production depends on precompilation of packs prior to booting for performance. compile: false # Extract and emit a css file extract_css: true # Cache manifest.json for performance cache_manifest: true
#javascript/packs/application.js // This file is automatically compiled by Webpack, along with any other files // present in this directory. You're encouraged to place your actual application logic in // a relevant structure within app/javascript and only use these pack files to reference // that code so it'll be compiled. require("@rails/ujs").start() require("turbolinks").start() require("@rails/activestorage").start() require("channels") require("jquery") require("bootstrap") require("jquery.raty") // Uncomment to copy all static images under ../images to the output folder and reference // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) // or the `imagePath` JavaScript helper below. // const images = require.context('../images', true) // const imagePath = (name) => images(name, true) import 'bootstrap'; import '../stylesheets/application'; import '../stylesheets/custom';

試したこと

cssは正常に動作しています。
画像もimage_pack_tagで読み込めています。
raty.jsは効いていません。

ひととおり記事は読み込んだのですが、
webpackerの使い方がいまいち分かっておりませんで、
助言いただけると助かります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

maisumakun

2020/04/14 05:10

raty自体の適用部分のコードはどのように書いていますか?(コードを書かないと有効にならないです)
ytommy

2020/04/14 05:19

あ、すみません。 そういうことではないですね。 適用部分のコードは下記です。 <div class="star-rating" data-score= <%= review.rating %> ></div> <script> $('.star-rating').raty({ readOnly: true, score: function() { return $(this).attr('data-score'); } }); </script>
maisumakun

2020/04/14 05:19

raty.jsは$().raty();のような形で呼ばないと有効になりません。それはどこへどのように書きましたか?
ytommy

2020/04/14 06:04

上記はviewファイルに直接<script></script>で書いています。
guest

回答1

0

ベストアンサー

適用部分のコードは下記です。

問題は2点あります。

  • require("jquery")のようにWebpack環境からjQueryを読み込んだ場合、jQueryインスタンスはWebpack環境に閉じてしまって、他のコードからは使えません。window.$ = window.jQuery = require('jquery');のようにwindow経由で公開する必要があります。
  • <script>を使ってスクリプトを書くような構成を取る場合、Turbolinksはマッチしません。require("turbolinks").start()の行を消しておきましょう。

投稿2020/04/14 05:23

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ytommy

2020/04/14 06:07

回答ありがとうございます。 window.$ = window.jQuery = require('jquery.raty');に変更したのですが、変化なしでした。 *require("turbolinks").start()はコメントアウトしました。
maisumakun

2020/04/14 06:17

> window.$ = window.jQuery = require('jquery.raty');に変更したのですが、変化なしでした。 そちらではなく、require('jquery')のほうです。
ytommy

2020/04/14 06:28

window.$ = window.jQuery = require('jquery');に変更してみましたが、変化なしでした。
maisumakun

2020/04/14 06:33

ブラウザのコンソールにメッセージは出ていませんか?
ytommy

2020/04/14 06:48

エラーが出ていました。(下記) Uncaught Error: Cannot find module 'jquery.raty' at webpackMissingModule (application.js:12) at Module../app/javascript/packs/application.js (application.js:12) at __webpack_require__ (bootstrap:19) at bootstrap:83 at bootstrap:83
maisumakun

2020/04/14 07:17

エラーメッセージのとおりです。yarn add jquery.ratyなどで追加する必要があります。
ytommy

2020/04/14 09:13

yarn add raty-jsで追加したところ解決いたしました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問