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开发者_如何学Go 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
精彩评论