2012-04-02 7 views
0
// main htm page from where aja x call happened 
    <div id ="vote_count_<%=answer.id %>" > 
    <%= render :partial => '/votes/count', :locals =>{ :answer => answer} %>//render a partial 
    <div id="wait" style=" float:center">// for loading image 
    </div> 
    </div> 

    // partial page :- count 

    <div class ="look6"> 
     <%= image_tag("thumbs_up.jpg", :alt => "like",:value => answer.id,:id => 'like',:class => 'vote')%>// image tag for like reload during ajax call 
     (<%= Vote.count(answer.id,'like')%>)// no of like vote 
     | <%= image_tag("thumbs_down.jpg", :alt => "unlike",:value => answer.id,:id  =>'unlike',:class => 'vote',:onclick=>"return vote()") %>// image tag for unlike reload during ajax call 
     (<%= Vote.count(answer.id,'unlike')%>)// no of unlike vote 
    </div> 


    // ajax function :- 

    <script type="text/javascript" > 

     function showLoadingImage() 
     { 

     $('#wait').append('<div id="loading-image"><%= image_tag("ajax-loader.gif", :alt=>"Loading...")%></div>');// for ajax loading 
     } 
    in first page: 
//.vote is a claas name 
     $(".vote").click(function(){ 
     alert("hi"); 
     var answer_id =$(this).attr("value");// for ans id 
     alert(answer_id); 
     showLoadingImage();// call loading image function 
     var result = $(this).attr("id");// whether like or unlike 
     $.ajax({ 

      cache: false, 
    //path for controller 
      url: '/create_vote/'+answer_id, 
      data: "result="+result,// data for ajax call 
      complete: function(){ 
      $('#loading-image').remove();// remove loading image 

      } 

     }); 
     cache: false; 
     return false; 
     }); 
    </script> 

    //ontroller: 

    def create_vote 
     @vote = Vote.new // create new vot 
     @vote.user_id = current_user.id// user id 
     @vote.answer_id = params[:id]// answer id 
     @vote.result = params[:result] == 'like' ? '1':'0'// like or unlike 
     answer = Answer.find(params[:id])// answer id find 
     if @vote.save// save vote 
     @message = "thanks"// message 
     else 
     @message = "sorry"// mesage 
     end 

     @vote_count = Vote.count(params[:id], params[:result])// total vote 
      respond_to do |format| 
      format.js { render '/votes/create_vote.js.erb', :locals => {:result =>params[:result],:answer =>answer}}// result return back 
      end 
     end 

///votes/create_vote.js.erb $( "#vote_count_ <% = AJAXリクエストを呼び出すイムそこから同じdiv要素をリロード"結果:>結果、:答え=>回答))%>"結果が表示されていない場合は、 )// js部品をレンダリングする私は最初のAJAX呼び出しでその作業を初めてAjaxの機能を呼び出すと、私は

//部分カウント <%= image_tag( "thumbs_up.jpg"、:alt => "like"、:value => answer.id、:id => 'like'、 :class => 'vote')%> (<%= Vote.count(answer.id、 'like')%>)|| ID:= ''と違う、:class => 'vote'、:onclick => "返品の票() ")%>

答えて

0

使用)%>(<% = Vote.count(answer.id 'とは異なり'): - の代わりに$(".vote").live("click", function(){

$(".vote").click(function(){

+0

その作業.. .. どうもありがとうございます –

関連する問題