简体   繁体   中英

Making a simple ajax post in jQuery and Ruby on Rails

I am trying to make a Ruby on Rails post via AJAX, and can't figure it out. So close!

= 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)

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.

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

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

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)

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