Monday, 2 June 2014

AJAX WITH JQUERY RAILS

AJAX WITH JQUERY RAILS

 routes.rb (i defined a customized method)

 get 'products' => 'products#reviews_lists', :as => 'reviews_lists'
 
in my root page: i.e,

show.html.erb
<%= javascript_include_tag "jquery-1.7.1.min" %>
<script type="text/javascript">
 jQuery(document).ready(function() {
    alert("saritha ");
    $('#review_list').change(function() {
        alert($('#review_list :selected').text());
        $.ajax({
              url: "<%= reviews_lists_path %>",
            data: {

              reviw_name : $('#review_list :selected').text(),
              pid: $('#pid').val(),

            },
            dataType: "script"
      });
    });
});
</script>

 <p>
  <%= select_tag :sort, options_for_select([["Newest  First",1],["1-5",2],["5-10",3]]), { :prompt => 'please select',:id=>"review_list", :pid=>'#{ @product.id}' }%>
   </p>
  <div id="saritha" class="review"> .... </div>

products_controller.rb

def reviews_lists
  @review_limit=params[:reviw_name].split('-')
    @reviews=Review.where("pid = ?", params[:pid]).order(:created_at).limit(@review_limit[1])
    respond_to do |format|
       format.js
    end
  end

reviews_list.js.erb

$('#saritha').html("<%= escape_javascript(render(:partial => 'show_review', :locals => {:reviews => @reviews})) %>");


 show_review.html.erb

<div class="clear"></div>
<% reviews.each do |review| %>
<div class="review">
       <p><strong><%= review.review_title %></strong></p>      
        <p><%= review.opinion %>.</p>
        <p>By <span><%= review.name %></span><br>from <%= review.location %> </p>
        <div class="review-bot">
            <p class="report"><a href="#">Report this comment</a></p>
            <p class="help">Was this review helpful?  <a href="#">Yes  /</a>  <a href="#">No</a></p>
        </div>
  </div>
 <% end %>





@@@@HAPPY RAILS CODING....


No comments:

Post a Comment