webpackerでerbを使うときにundefined method `image_pack_tag' for main:Object (NoMethodError)が発生した場合の解決方法

スポンサーリンク

f:id:ihatov08:20160926230350j:plain

gemがもつcssをwebpackerで使用したくて↓の記事を参考にpacks/application.js.erbを使用した。

blog.piyo.tech

// 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")
import '../src/application.css'
window.$ = window.jQuery = require("jquery")

import UIkit from 'uikit';
import Icons from 'uikit/dist/js/uikit-icons';
UIkit.use(Icons);
// require("uikit/dist/js/uikit.js")
// require("uikit/dist/js/uikit-icons.js")
// 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)

require("trix")
require("@rails/actiontext")// Support component names relative to this directory:
var componentRequireContext = require.context("components", true);
var ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);

しかし

undefined method `image_pack_tag' for main:Object (NoMethodError)

のエラーが発生する。 erbにするとコメント部分の<%= image_pack_tag 'rails.png' %> もerbとして解釈されてしまうようだ。 コメントアウトを削除したら解決した。

after

// 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")
import '../src/application.css'
window.$ = window.jQuery = require("jquery")

import UIkit from 'uikit';
import Icons from 'uikit/dist/js/uikit-icons';
UIkit.use(Icons);

require("trix")
require("@rails/actiontext")// Support component names relative to this directory:
var componentRequireContext = require.context("components", true);
var ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);