I am trying to make a Ruby on Rails post via AJAX, and can’t figure it out. So close!
HTML:
= link_to image_tag(current_user.votes.any? {|v| v.votable_id == post.id && v.value > 0} ? 'vote-up-selected.jpg' : 'vote-up.jpg'), vote_up_post_path(post)
CONTROLLER:
def vote_up
get_vote
@vote.value += 1 unless @vote.value == 1
@vote.save
respond_to do |format|
#format.html { render :action => 'vote_up', , :notice => "Voted Up." ) }
end
end
Sorry, this is a dumb question. I’m more so asking as to what the ‘best’ practice to doing this is.
route_name_pathisn’t gonna return ‘post’ link, even ifroute_nameis specified with post requirement. However,link_tomethod does take:methodoption. I never used, but they claim it creates hidden form to submit request with passed http verb:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#M002142
But switching to get might be easier: at least, you won’t have to mess with authenticity_token. (not sure if it’s generated by default with that form)