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.

1
  • No error. I can get this to work just fine if it renders an entire page, or even redirects back to its original page after submit. The trick, I guess, is just getting into to send a POST request without reloading. Commented Aug 9, 2010 at 23:15

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you FD, that was very informitive.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.