Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9101331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:06:09+00:00 2026-06-17T01:06:09+00:00

So I have a posts scaffold generated in a Rails app and I’ve added

  • 0

So I have a posts scaffold generated in a Rails app and I’ve added an upvote and downvote column to the post model. I added an “upvote” button on the view file and I need to make an AJAX call and query the database when you hit the upvote button, but the upvote button has no real Rails <form> attached to it. How can I make this AJAX call and add the upvote to the database for the upvoted post?

When I make this AJAX call:

$('.up,.down').click(function(){
    $.ajax({
      type: 'POST',
      url: '/posts',
      dataType: 'JSON',
      data: {
        post: {
          upvote: 1
        }
      },
      success: function(){
        alert('success')
      }
    });
  });

It returns a 500 error. Where do I go form here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:06:11+00:00Added an answer on June 17, 2026 at 1:06 am

    You could use the :remote => true attribute on the link_to helper
    for example:

    <%= link_to post_upvote_path(post), :remote => true, :method => "put" %>
    <%= link_to post_downvote_path(post), :remote => true, :method => "put" %>
    

    then in config/routes.rb:

    resources :posts do
      put "upvote", :to => "posts#upvote", as: :upvote
      put "downvote", :to => "posts#downvote", as: :downvote
    end
    

    then handle the voting in your posts controller, like you probably already are and grab the post id with params[:id] in the action

    Here is an intro to rails flavored unobtrusive javascript

    Update
    To see the upvote and downvote routes that were created, go to the terminal and type

    rake routes | grep vote
    

    this will give you a list of all of your routes that have “vote” in the name. Or just type rake routes to get a list of all of them. The first column is the named route, just append ‘_path’ to the end of it to use it in your app – like post_upvote_path above would be seen as

    post_upvote  PUT  /posts/:id/upvote(.:format) posts#upvote
    

    And in you PostsController you would want these actions:

    class PostsController < ApplicationController
      ###
      # index, show... other RESTful actions here
      ###
    
    
      def upvote
        @post = Post.find params[:id]
        # code for however you are voting up the post here
      end
    
      def downvote
        @post = Post.find params[:id]
        # code for however you are voting down the post here
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created model, view and controller: $ rails generate scaffold Post name:string title:string
I am building a toy Rails app. I generated a scaffold for my Post
Let's say I have Posts and Comments for a blog app using Rails and
I have simple application on rails with Mongodb(Mondoid mapper). Scaffold posts , and I
So here is my dilemma. I have created a rails scaffold named posts for
The command rails generate scaffold Post name:string title:string content:text generated the following 20101109001203_create_posts.rb file:
I have a rails application where I generated some scaffold for a title (string)
I have a simple .ajax post to a rails scaffold project running on Heroku:
I have a posts model... I am trying to display this model on the
I have three tables: Post, Attachment, and Media. Posts have Attachments, and Attachments have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.