Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting undefined method `javascript_pack_tag' for ... #172

Closed
schovi opened this issue Mar 14, 2017 · 33 comments
Closed

Getting undefined method `javascript_pack_tag' for ... #172

schovi opened this issue Mar 14, 2017 · 33 comments

Comments

@schovi
Copy link

schovi commented Mar 14, 2017

Having Rails 5 application and followed this tutorial https://medium.com/statuscode/introducing-webpacker-7136d66cddfb#.4xpiogt9l how to install and run webpacker.

Having webpacker in Gemfile

gem 'webpacker', github: 'rails/webpacker'

And used installation commands

rails webpacker:install
rails webpacker:install:react

And when open page with <%= javascript_pack_tag 'app' %> I am getting undefined method 'javascript_pack_tag' for #<#<Class:0x007f8d5a158378>:0x007f8d5a0015b0>

./bin/webpack-dev-server runs fine (but having some babel errors, but I guess that can't have impact on previous error).

@ytbryan
Copy link
Contributor

ytbryan commented Mar 15, 2017

It works on my side. I'm using rails 5.1.0.beta1, latest webpacker from github.

Did you bundle install?

Not sure what error you got for webpack-dev-server, but if your port is blocked, you will encounter error like [Error] Failed to load resource: the server responded with a status of 404 (File Not Found) (hello_react.js, line 0). The solution is to edit development.rb and change the port from 8080 to something else (ie 8081)

Let me know if the problem is resolved.

@schovi
Copy link
Author

schovi commented Mar 15, 2017

@ytbryan Sure I run bundle install

Using webpacker 1.0 from git://github.com/rails/webpacker.git (at master@a85a3ab)

Tutorial said it is for Rails 4.2+ and unfortunately due to dependency hell in my project I can't update to 5.1 yet :(

@schovi
Copy link
Author

schovi commented Mar 15, 2017

I traced the gem source code webpacker/lib/webpacker/railitie.rb

class Webpacker::Engine < ::Rails::Engine
  initializer :webpacker do |app|
    p "Initializing webpacker"
    ActiveSupport.on_load :action_controller do
      p "Using Webpacker::Helper"
      ActionController::Base.helper Webpacker::Helper
    end

and it printed my debug, but still same error.

Then I tried to use Helper manually and everything is fine.

class ApplicationController < ActionController::Base
  helper Webpacker::Helper
end

@ytbryan
Copy link
Contributor

ytbryan commented Mar 15, 2017

May I know which rails version is this project on?

@schovi
Copy link
Author

schovi commented Mar 15, 2017

@ytbryan Rails 5.0.1

@ytbryan
Copy link
Contributor

ytbryan commented Mar 15, 2017

Just tested it with 5.0.1 and it still works fine on my end.

@schovi
Copy link
Author

schovi commented Mar 15, 2017

Maybe some conflict gem?

@ytbryan
Copy link
Contributor

ytbryan commented Mar 16, 2017

I don't have enough info to confirm that.

Can you close this if the manual helped fixes it? Feel free to open it again if you find the root of the problem.

@schovi
Copy link
Author

schovi commented Mar 16, 2017

Thanks

@schovi schovi closed this as completed Mar 16, 2017
@gauravtiwari
Copy link
Member

@schovi Were you using .js.erb extension for app pack?

@gauravtiwari
Copy link
Member

If yes to above, do you got javascript comments in that file? Please remove that since it's erb. I have made a demo for you here - https://tranquil-inlet-61365.herokuapp.com/ and code is here - https://github.com/gauravtiwari/webpacker-demo-app

@schovi
Copy link
Author

schovi commented Mar 17, 2017

@gauravtiwari I have bare structure generated by rails webpacker:install and rails webpacker:install:react nothing more.
And I think problem is strictly related to Rails and its Helpers, because of manaully using

class ApplicationController < ActionController::Base
  helper Webpacker::Helper
end

works fine.

@gauravtiwari
Copy link
Member

Ah right, but it's bit strange too, given you are using Rails 5.0.1. It works without including helper for me - https://github.com/gauravtiwari/webpacker-demo-app. Perhaps, application.rb is setup differently - I mean includes on the top that adds gems from Rails?

@schovi
Copy link
Author

schovi commented Mar 17, 2017

@guilleiguaran

require_relative 'boot'

require 'active_record/railtie'
require 'action_controller/railtie'
require 'action_view/railtie'
# require 'action_mailer/railtie'
# require 'action_cable/engine'
# require 'rails/test_unit/railtie'
require 'sprockets/railtie'
require 'sidekiq/api'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module MyApp
...

@gauravtiwari
Copy link
Member

@schovi Could you please add require 'webpacker/railtie' in the list and then retry running without including helper in the controller? The one I have got require 'rails/all', which means it adds everything.

@schovi
Copy link
Author

schovi commented Mar 18, 2017

@gauravtiwari

When adding require 'webpacker/railitie' it end up on missing constant Webpacker

/Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/source.rb:5:in `<top (required)>': uninitialized constant Webpacker (NameError)
	from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/helper.rb:1:in `require'
	from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/helper.rb:1:in `<top (required)>'
	from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/railtie.rb:3:in `require'
	from /Users/schovi/.rvm/gems/ruby-2.4.0@lotof/bundler/gems/webpacker-a85a3ab0eac6/lib/webpacker/railtie.rb:3:in `<top (required)>'
	from /Users/schovi/work/lotof/config/application.rb:14:in `require'

This fixes it:

module Webpacker
end
require 'webpacker/railtie'

but still undefined method 'javascript_pack_tag'. Even when require 'rails/al' as default.

When I will have time, i will try to remove 3rd party gems one by one to find if there is conflict.

@gauravtiwari
Copy link
Member

@schovi I see. It's require 'rails/all' actually. Perhaps, just a typo in comment.

@schovi
Copy link
Author

schovi commented Mar 18, 2017

@gauravtiwari just typo, sorry.

@lbrito1
Copy link
Contributor

lbrito1 commented Mar 29, 2017

@schovi I am having the same issue, even after adding your snippet:

require_relative 'boot'

require 'yaml'
require 'rails/all'
module Webpacker
end
require 'webpacker/railtie'

Bundler.require(*Rails.groups)

Did you find a solution to this? (that does not involve monkey-patching/manually defining helpers)

@schovi
Copy link
Author

schovi commented Mar 29, 2017

@lbrito1 it was not solution. It was just experimenting with custom requiring. Snippet from before was workaround #172 (comment)

@modusss
Copy link

modusss commented Apr 11, 2017

I have the same error here with rails 5.0.1 and what solved to you didn't change here...

@archonic
Copy link

archonic commented Apr 11, 2017

I'm not sure why, but I found this problem was introduced when I included gem 'jquery-rails' and bundled. Update: That seems to have just been a result of an earlier error showing first.

@archonic
Copy link

archonic commented Apr 12, 2017

A generated Rails 5.1.rc1 with --webpacker is working fine. I've updated a Rails 5.0 app and installed the webpacker gem. At one point it was working and I'm not sure what change broke the javascript_pack_tag method. I've compared the generated app to my own and found a few inconsistencies. Fixing those inconsistencies hasn't fixed it though.

Here's the repo: https://github.com/archonic/limestone. My eternal gratitude if you can find what the issue is.

Update: It's the strangest thing. I can insert a byebug and call Webpacker::Helper.methods and it doesn't include any of the methods that are clearly defined in my local gem files which are clearly loaded by bundler. Until that's figured out I've "fixed" it by using javascript_include_tag Webpacker::Manifest.lookup('application.js'), 'data-turbolinks-track': 'reload'

@tylerhunt
Copy link

I ran into this issue, too, and in my app traced it to a config.before_action definition in the Active Admin initializer. I dug into it, and my best understanding of the issue is that this was causing ActionController::Base to be loaded prematurely, and this was somehow preventing the Webpacker helpers from being loaded properly (they were available under ActionController::Base.helpers but not in the views).

@schovi
Copy link
Author

schovi commented Apr 26, 2017

@tylerhunt I have ActiveAdmin ofcourse.

@kernow
Copy link

kernow commented Apr 26, 2017

I've also run into this issue on rails 5.1.0.rc2 the project is using active admin but it doesn't use any before_action configuration. I've not had a change to look into what is causing this as yet and the work around fixes the issue for now. If I get a chance to look into this more deeply I'll report back here.

guilleiguaran added a commit that referenced this issue Apr 26, 2017
Include webpacker helper to action view #172
@gauravtiwari
Copy link
Member

Please could you all try latest master - bundle update webpacker. This issue is now fixed 🎉 🍰

@archonic
Copy link

Fixed, thanks!

@sratsimba
Copy link

Don't forget to restart your server :trollface:

@emcoding
Copy link

For prosperity:
I got a message in the console that I had to update yarn (yarn install) and that fixed the error.
(Rails 5.2.2, webpacker 3.5.5)

@tyler-boyd
Copy link

I ran into a similar issue when adding webpacker erb support.

For me (and hopefully anyone reading this), the issue was caused by the js-style comment (as suggested earlier by another comment):

// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

The solution is to make sure there is no <% ... tags anywhere in the code, including JS comments.

@chrisdetmering
Copy link

I had this same problem. I just restarted my server and it worked.

pacstudios added a commit to pacstudios/Blog that referenced this issue Apr 22, 2020
Поставил сервер, исправил косяк с запуском (решение нашел здесь - rails/webpacker#172 (comment)). Релаизовано: CRU (из CRUD) - Create, Read, Update.
@fakefarm
Copy link

I had to stop the server, then run bin/spring stop then rails s
Then, the javascript_pack_tag worked.
(Restarting the server wasn't sufficient.)

But what has me curious is that I didn't need to start bin/webpack-dev-server
(I thought that was my problem originally) I am curious when, and where, that is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests