1
votes

I am trying to create a news feed for my rails app. However I am running into an issue with updating the number of news stories being shown. The following code is from a partial in my views.

I want to re-render the partial 'recent_news_content' in the div of the same id when a certain div is scrolled to the bottom. However when I escape javascript in order to re-render, I cannot access my newsLimit variable and I do not know how (and don't think I can) modify @news_limit or create a new ruby variable from HAML (or any view for that matter) to access in my re-rending statement.

%h4#recent-news-header.center.top-header Recent News
  %div#recent-news-box.bordered.selectable
    %div#recent-news-content
      = render :partial => "recent_news_content", :locals => {:news_limit => @news_limit}

:javascript
  $(document).ready(function() {
    var newsLimit = @news_limit
    $("#recent-news-box").bind('scroll',function(e) {
      var elem = $(e.currentTarget);
      if (elem[0].scrollHeight - elem.scrollTop() < elem.outerHeight()) {
        newsLimit += 5;
        $('#recent-news-content').html("#{escape_javascript(render :partial => 'recent_news_content', :locals => {:news_limit => newsLimit})}");
      }
    });
  });

The last newsLimit is not being recognized (because I escaped javascript) and I can't figure out how to make javascript replace it with its value before sending it to render :(.

Also I realize it is not the best style to have functional logic such as this in the view, and I am open to suggestions.

1

1 Answers

0
votes

I managed to solve this with an AJAX server call, to explicitly assign the necessary value for @news_limit each time the user scrolls to the bottom of the feed.

I used techniques similar to: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html