Saturday, 31 August 2019

Infinite Scrolling with Ajax in Ruby on Rails.

In controller.rb

Add the pagination with all records in index method

def index
  @listing = Listing.paginate(:per_page=>10, :page=>params[:page])
end

In index.html.erb

Add scrolling script and partial file for appending the display data in index file.
       
 <div class='main'>
 <div id='listing_data'%>
      <%= render partial: 'list_info', locals: {listing: @listing} %>
  </div>


  <input type='hidden' id='next_page_count' value='2'>

  <div class="pagination display_none"></div>
  <div class="display_none">

      <a class="btn view_more" href="#" data-remote="true">Loading</a>
  </div>

 </div>

<script type='text/javascript'>
var queryString = window.location.search;
var pathname = window.location.pathname;
var url = pathname + queryString;

pagination_url = queryString.length > 0 ? url+'&page=<%=@page['next_page']%>' : url+'?page=<%=@page['next_page']%>';
$('.view_more').attr("href",pagination_url);


 var count = $('#
next_page_count').val();
       
 $(window).scroll(function(){
      if  ($(window).scrollTop() == $(document).height() - $(window).height()){
          //if the next page is empty stop ajax calling
            if ($('#next_page_count').val() == ""){
                 return false;
            }else{
                  loadActivity();
            }
       }
    });


  #ajax callling       

  function loadActivity() {
      $('.view_more').click();     
    }
</script> 


In index.js.erb

Add the rendering data here.

$('#next_page_count').val('<%=@page['next-page']%>');

var queryString = window.location.search;
var pathname = window.location.pathname;
var url = pathname + queryString;

pagination_url = queryString.length > 0 ? url+'&page=<%=@page['next_page']%>' : url+'?page=<%=@page['next_page']%>';
$('.view_more').attr("href",pagination_url);


<%if @page['current_page'].to_i == 1 && @page['total_pages'].to_i > 0 %>
 $('#listing_data').html('<%= escape_javascript(render partial: 'list_info', locals: { listing: @listing }) %>');
<%else%>
 $('#listing_data').append('<%= escape_javascript(render partial: 'list_info', locals: { listing: @listing }) %>');
<%end%> 


Whenever scrolling is happening need to call the next page with ajax calling.if params[:next_page] is null stop the ajax calling.




Monday, 26 August 2019

Find the missing elements and display the sorted elements in ruby?

Inputs:
a=[1,2,3,4,5,6,7,8,9,10]
b=[1,2,4,5,6,7,8,9,10]

missing_elements = []
for i in a.min..a.max
    missing_elements.push(i) unless b.include?(i)
end

missing_elements.each do |m|
    b.insert(a.index(m),0)
end
p a
p b

for i in 0..a.length-1
puts "#{a[i]}-#{b[i]}"
end

OUTPUT:
1-1
2-2
3-0
4-4
5-5
6-6
7-7
8-8
9-9
10-10

How to find the missing elements in an array using ruby?

Input: a= [3, 4, 5, 6, 7, 15]

min_element  = a.min
max_element = a.max
new_missing_elements = []

for i in (min_element..max_element)
   new_missing_elements.push(i) unless a.include?(i)
end

print new_missing_elements

OUTPUT:

=>[8, 9, 10, 11, 12, 13, 14]

Friday, 2 August 2019

Class method calling in ruby?

Class methods:
We can call the class method by using class name.

class MyClass
   def self.my_method
     puts 'i am class method'
  end
end

 
 
MyClass.my_method
output=>
i am class method

Tuesday, 9 July 2019

How to find a given number is prime or not in ruby?

def check_prime n
    (2..n-1).each do |no|
         return false if n % no == 0
    end
    true
end

Tuesday, 2 July 2019

How to multiply the array elements in ruby?

a= [1,2,3,4,5,6,7,8]

Scenarios 1:
a.reduce(:*)

Scenarios 2:
b=1

a.each do |e|
  b=b*e
end


Factorial program using ruby

def factorial_no
  yield
end

n=gets.to_i
factorial_no do
  (1..n).reduce(:*)
end


Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...