Js.erb works locally but doesn't work

I followed the tutorial here http://railsforbeginners.com/chapters/chapter-9-infinite-scroll/ for infinite scrolling. The code works well locally, but when I deploy it to prod. page links (1 2 3 4) are still displayed and endless scrolling does not work. I also tried adding these files to assets.rb without success

First I use Rails 4 , my application.js as follows

 //= require jquery2 //= require jquery.turbolinks //= require jquery_ujs //= require jquery-ui.min //= require bootstrap-hover-dropdown.min //= require bootstrap.min //= require select2 //= require infinite_scroll //= require turbolinks 

Controller action

 respond_to do |format| format.html format.js { render "visitors/index" } end 

index.js.erb

 $('#my-articles').append('<%= j render @articles %>'); <% if @articles.next_page %> $('.pagination').replaceWith('<%= j will_paginate @articles %>'); add_tweets(); <% else %> $(window).off('scroll'); $('.pagination').remove(); <% end %> function add_tweets(){ <% @articles.each do |article|%> handle_open_modal("<%= article.id %>"); <%end%> } 
+5
source share
3 answers

Here is what I did to get it working using this link

I removed the turboprops and all this link

Then we added this to index.html.erb without using jQuery $(document).on('ready

 if ($('#infinite-scrolling').size() > 0){ $(window).on('scroll', function(){ var more_posts_url = $('.pagination .next_page a').attr('href') heights = $(document).height() - $(window).height() - 500 if(more_posts_url && $(window).scrollTop() > heights){ $('.pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />') $.getScript(more_posts_url) } }); } 

Then all *.js.erb will start working.

I tried to avoid removing turbolinks, but tried to get it to work with it.

0
source

In assets.rb

Try this line:

 config.assets.precompile += ['Index.js'] 

-> Rails can only refer to files using its own name (not ERB)

Also, as a style, you must enter the file name in lowercase: index.js.erb

0
source

Are you sure jQuery is loading? As suggested above, let's move on to the developer of the Network tool and reboot. Are your assets precompiled in the production environment? Make sure this config.serve_static_assets = true in your config / application.rb For Heroku and Rails 4 see here

0
source

Source: https://habr.com/ru/post/1258240/


All Articles