简体   繁体   中英

Rails 3 Ajax success message

I have a voting system that i'm trying to Ajax.

In the view i have:

<div id="yes"><%=button_to 'Vote',vote_path(c),:id=>c.id,:remote=>true,:html=>{:class=>"vote"}%></div>

and the controller goes:

`def vote

    number=Video.find(params[:id])
    number.increment!(:votes)
    if number.update_attributes(params[:votes])
       flash[:notice] = "Vote Recorded Successfully."

    render :text => number.votes  

   end`

When a user votes, i want to hide the vote button and display a message like "Thanks for Voting".

So, in my application.js, i have

$(document).ready(function(){
    $(".vote").bind("ajax:success",function(evt,xhr,settings){
       var $ent=$(this);
        $ent.find(("div.yes")).hide(100);
    }
    }
);

Please point me to some resource to accomplish this.Thank you.

You have to return something jQuery can handle, plain JSON works fine:

def vote
  ...
  render :json => {:votes => number.votes}
end

Instead of rendering :text, render a erb.js view. This is called unobtrusive javascript.

See here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM