views:

70

answers:

2

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.

A: 

route_name_path isn't gonna return 'post' link, even if route_name is specified with post requirement. However, link_to method does take :method option. 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)

Nikita Rybak
+1  A: 

I recommend checking out Ryan's railscast about using jQuery with Rails:

http://railscasts.com/episodes/136-jquery

fd
Thank you FD, that was very informitive.
Trip