Thursday, 16 April 2015

Pagination With Ajax Using Will Paginate in Ruby on Rails



step1: I have and index page. TO display all records.

controller method:
    def index
      @products = Product.paginate(:order =>"name ASC" ,:page => params[:page], :per_page => 14)
      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @products }
        format.js
      end
    end


step2: i have a dorp down with type1 ans type2 categories.
index.html.erb

<h1>Products</h1>
<%= select_tag :type, options_for_select([["type1",1],["type2",2]]), { :prompt => 'Select Category', :class => 'form-control', :id =>"type", :style => "width: 24%;margin-bottom: 7px;"} %>
<div id="products">
 <%= render "products/products" %>
</div>

<script>
$( document ).ready(function() {
    $("#type").change(function() {
        var type = $('select#type :selected').val();
        $.ajax({
            url: '/admin/index/'+type,
                dataType: "script"
        });
    });
    $(function(){
           $('.pagination a').attr('data-remote', 'true')
    });
});
</script>

step 3:
index.js.erb

jQuery('#products').html(" 'products/products' )%>");
$('.pagination a').attr('data-remote', 'true');

step 4:
_products.html.erb
<div class="listing">
      <% @products.each do |product| %>
        <div>
     <span> <%= product.title %> </span>
           <span> <%= product.price %>  </span>
        </div>
     <% end %>
</div>
<%= will_paginate @products %>


Thats all. ajax based pagination is done....

Thursday, 9 April 2015

Mongoid scopes usage

class Person
  include Mongoid::Document
  field :occupation, type: String
  field :age, type: Integer

  scope :rock_n_rolla, -> { where(occupation: "Rockstar") }
  scope :washed_up, where(:age.gt => 30)
  scope :age_or_occupation, ->  {any_of({occupation: "Rockstar"}, {age: 18}) }
  scope :over, ->(limit) { where(:age.gt => limit) }
end

# Find all the rockstars.
Person.rock_n_rolla

# Find all rockstars age greater than 30.
Person.washed_up.rock_n_rolla

# Find all rockstars or other persons have their age as 18 (or operation)
Person.age_or_occupation

# Find a criteria rockstart with 60 as their aget.
Person.rock_n_rolla.over(60)

Monday, 30 March 2015

Two will_paginate objects in single page:




in controller:

@users = User.all.paginate(:page => params[:page], :per_page => 5)
@clients = Client.all.paginate(:page => params[:page], :per_page => 5)

in view:
<%= will_paginate @users %>
<%= will_paginate @clients %>

it will take params[:page] as param so when click on any of pages of client or use..both will navigate to another page.

Insted of that,

@users = User.all.paginate(:page => params[:user_page], :per_page => 5)
@clients = Client.all.paginate(:page => params[:client_page], :per_page => 5)

in view:
<%= will_paginate @users , :param_name => 'user_page'%>
<%= will_paginate @clients, :param_name => 'client_page' %>

Now it will work perfectly...

Friday, 27 February 2015

html5 date picker in mozilla

<script src="//cdn.jsdelivr.net/webshim/1.14.5/polyfiller.js"></script>;
<script>
webshims.setOptions('forms-ext', {types: 'date'});
webshims.polyfill('forms forms-ext');
</script>

Friday, 5 September 2014

HOW TO APPLY DIFFERENT LAYOUTS FOR SAME CONTROLLER

class Events::EventsController < AuthenticatedController
layout :user_layout
#for admin
def new
end
def show

end
#for classifieds layout
def classifieds

end
protected
def user_layout
if params[:layout]=="admin"
"admin"
else
"classifieds"
end
end

end

Here i am applaying admin layout to new and show, classfieds to classifieds..

Inorder to apply those two layouts we need to pass layout="admin" as a paramater, in the same way layout="classifieds" for classifieds layout..

Ex: <li><a href="/events/new?layout=admin"><i class="fa fa-angle-double-right"></i>Events</a></li>
end

Wednesday, 3 September 2014

MONGOID SEARCH

1, add gem to your gemfile
gem 'mongoid_search'

2, bundle install
3, include below statements to ur model event.rb
include Mongoid::Search
search_in :title,:venue,:launch_date,:tags,:description,:event_type
3, create 2 routes one for post and one for get

routes.rb
get 'search/:q' => 'events_user#search_result'
post 'search' => 'events_user#search_query'

4,create below methods in your events_user controller
def search_query
if search_params[:q].empty?
redirect_to :back
else
redirect_to '/events/search/' + search_params[:q]
end
end
def search_result
@search_result = Events::Event.full_text_search(params[:q]).paginate(:page => params[:page], :per_page => 10)
@latest=Events::Event.desc(:created_at).limit(10)
@q = search_params[:q]
end
private
def search_params
params.permit(:q)
end
5, write related view code in u r search_result .html.erb

<% @search_result.each do |event| %>
<div class="details-innercontent">
<div class="media">
<%= link_to(image_tag("#{event.banner_image.url}",:class=>"img-responsive" ,:alt=>"banner"),events_details_path(:id=>event.id),:class=>"pull-left") %>
<div class="media-body">
<div class="top-content">
<%= link_to raw(event.title),events_details_path(:id=>event.id), :class=>"media-heading" %>
<p><%=h truncate(event.description, :length => 80) %></p>
<% @tags=event.tags.split(",") %>
<% for tag in 0...@tags.length %>
<%= link_to @tags[tag], "#", :class=>"drama" %>
<% end %>
</div>
<div class="bottom-content">
<span class="pull-left"><%= event.created_at.strftime("%d %b %y") %></span>

</div> </div> </div> </div> <% end %>

Enjoy...............:-)

Monday, 1 September 2014

Find all records for a given month

Here I have month and year as params...these two are change based on select box value....

Here i need to find records with in month choosen from select box....

Rails Query:
year=params[:month_name].split(",")[0].to_i
month=params[:month_name].split(",")[1].to_i
date=DateTime.new(year,month)
@all_events=Events::Event.where(:created_at => date.beginning_of_month..date.end_of_month)

From console:(Better understanding)
2.1.1 :005 > date=DateTime.new(2013,02)
 => Fri, 01 Feb 2013 00:00:00 +0000
2.1.1 :006 > date.beginning_of_month..date.end_of_month
 => Fri, 01 Feb 2013 00:00:00 +0000..Thu, 28 Feb 2013 23:59:59 +0000