I'm using Rails 6 with bootstrap and webpack. everything was working fine. Bootstrap works, jQuery works, etc until..
I'm following this acts_as_votable tutorial: https://blog.makersacademy.com/how-to-make-a-dynamic-rails-like-unlike-button-using-the-acts-as-votable-gem-9ab71686ff82
When the PostController likes the post it goes into the like.js.erb and then in the browser returns the error
Uncaught ReferenceError: $ is not defined
at <anonymous>:1:1
at processResponse (rails-ujs.js:282)
at rails-ujs.js.195
at XMLHttpRequest.xhr.onreadystatechang (rails-ujs.js:263)
jQuery is working though as I have bootstrap working with no errors. its only when it comes in thru the ajax that it seems to be missing it.
in PostsController:
respond_to :js, :html, :json
def like
@post = Post.find(params[:id])
if params[:format] == 'like'
post.liked_by current_user
elsif params[:format] == 'unlike'
post.unliked_by current_user
end
end
in like.js.erb:
$('.liked-btn').on('ajax:success', function(){
$(this).parent().parent().find('.like-count').html('<%= @post.get_likes.size %> &128591');
$(this).closet('.liked-btn').html('Like');
});
$('.like-btn').on('ajax:success', function(){
$(this).parent().parent().find('.like-count').html('<%= @post.get_likes.size %> &128591');
$(this).closet('.like-btn').html('Liked');
});
The 'likes' appear to be registering its just the return ajax and javascript that errors out. The routes seem fine as per the tutorial.
One funny thing is in my controller i mistyped "respond_to" as "responds_to" and it gave the error @posts.get_likes method not known for nilClass. Which means it did not blow up on $ symbol and understood jquery just fine in that instance. so something in the controller is causing this? (never mind on the funny thing it was probably just the ERB getting processed before the javascript was being run)
Note: I also tried replacing $ with jQuery and got the same error, except with jQuery unknown at that time.