"#{@partial_name}")) %>");But then I adde" name="description" /> "#{@partial_name}")) %>");But then I adde" />
6140

JS controlling ajax tabs and JS controlling will_paginate in same js.erb not working (endless scroll

Question:

I have tried to fix this for some time, but I cannot find the solution. I have ajax controlling some on page tabs, which is working fine:

$("#feed-content").html("<%= escape_javascript(render(:partial => "#{@partial_name}")) %>");

But then I added will_paginate with endless scrolling, where I put the js that is controlling that in the same js.erb file as the above (sale.js.erb):

$("#feed-content").html("<%= escape_javascript(render(:partial => "#{@partial_name}")) %>"); $('#products').append('<%= escape_javascript(render(:partial => 'sale_content', :products => @products, :remote => true)) %>'); <% if @products.next_page %> $('.pagination').replaceWith('<%= escape_javascript( will_paginate(@products)) %>'); <% else %> $('.pagination').remove(); <% end %>

But that do not working. But each part work individually.

Then i tried this, but it still does not work (it only loads the if part):

<% if params[:feed]%> $("#feed-content").html("<%= escape_javascript(render(:partial => "#{@partial_name}")) %>"); <% else %> $('#products').append('<%= escape_javascript(render(:partial => 'sale_content', :products => @products, :remote => true)) %>'); <% if @products.next_page %> $('.pagination').replaceWith('<%= escape_javascript( will_paginate(@products)) %>'); <% else %> $('.pagination').remove(); <% end %>

<strong>the view</strong>

... <p class="hero-description-dark local-nav-container">Sort by <%= link_to "popular", products_popular_path(:feed => "popular"), :remote => true, :class => "active"%> or <%= link_to "sale", products_sale_path(:feed => "sale"), :remote => true%>

<strong>Controller</strong>

def sale products = Product.gender(current_user).available.includes(:likes) @products = products.filter_by_categories(products).first(100).paginate(:page => params[:page], :per_page => 6) @partial_name = "sale" respond_to do |format| format.html format.json { render json: @products} format.js end end

Answer1:

there is a missing <% end %> tag. I which that is not the problem. Try something like this:

<% if params[:feed].present? %> $("#feed-content").html("<%= escape_javascript(render(:partial => "#{@partial_name}")) %>"); <% else %> $('#products').append('<%= escape_javascript(render(:partial => 'sale_content', :products => @products, :remote => true)) %>'); <% if @products.next_page %> $('.pagination').replaceWith('<%= escape_javascript( will_paginate(@products)) %>'); <% else %> $('.pagination').remove(); <% end %> <% end %>

Answer2:

I found a solution myself. I added ID's to my "remote true"-links like this. In this case "popular":

Sort by <%= link_to "popular", products_popular_path(:feed => "popular"), :remote => true, :class => "active", :id => "popular"%>

Then in the corresponding popular.js.erb file I added an onclick event to the ajax that controls the tabs. This means that the ajax only runs when the link is clicked at not when the page is supposed to do the endless-scrolling part.

$("#popular").click(function() { $("#feed-content").html("<%= escape_javascript(render(:partial => "#{@partial_name}")) %>"); }); $('#products').append('<%= escape_javascript(render(:partial => "#{@partial_name}")) %>'); <% if @products.next_page %> $('.pagination').replaceWith('<%= escape_javascript( will_paginate(@products)) %>'); <% else %> $('.pagination').remove(); <% end %>

There may be a better and cleaner way, but this worked for me. I have asked another question <a href="https://stackoverflow.com/questions/21143491/error-in-ajax-tabs-when-using-endless-scroll-with-rails-will-paginate" rel="nofollow">here</a> that is almost the same as this one. Just if someone needs more details.

<strong>UPDATE:</strong>

The above solution required two clicks on link to render the partial. The code beneath should be used instead:

$('#popular').bind('ajax:complete', function() { $("#feed-content").html("<%= escape_javascript(render(:partial => "popular_content")) %>"); });

Recommend

  • Cause of slow Scrapy scraper
  • endless scrolling does not work
  • AttributeError: 'module' object has no attribute 'choice'
  • Reset bootstrap modal
  • How to include array content in HTML?
  • Toggle dropdown mini-cart when product added to basket
  • Website flashes between loads
  • How do you submit a rails 3 form without refreshing the page?
  • Using nativescript converters
  • why won't rails find rabl templates?
  • Cleaning up view ruby logic and separating concerns into model/controller
  • Performance issue for Rails: How to send gzip assets
  • rails link_to_add_fields not adding fields with has_many :through (with nested form inside)
  • Using STAT relation in CActiveDataProvider criteria
  • Angular Material - Getting index of row in data table
  • NoMethodError / undefined method `foobar_path' when using form_for
  • Creating an array of products in JSON-LD
  • Template is missing error even if I have done a redirect_to in the controller
  • didSelectRowAtIndexPath method at PFQueryTableViewController
  • Rails has_many :through association: save instance into join table
  • fetch all links under/in a specific class-selenium webdriver (java)
  • how to add semantic ui in a rails app?
  • Cannot style mat-tab without ::ng-deep and !important
  • Can my app be notified when another application starts/stops playing audio?
  • Loading .coffee files via a view in Rails
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • SignalR .NET Client Invoke throws an exception
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Azure Cloud Service Web Role web pages do not load
  • Why winpcap requires both .lib and .dll to run?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to disable jQuery.jplayer autoplay?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?