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 6220087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:53:47+00:00 2026-05-24T07:53:47+00:00

I have queried on user posts (nested resources, obviously) and have a long list

  • 0

I have queried on user posts (nested resources, obviously) and have a long list of posts from different users. I would like the user to be able to click a little star next to each post to favorite that particular post through ajax. Any suggestions on how to achieve this? The trouble I’m running into is multiple favorite buttons on one page, favoriting multiple posts.

This is sorta like Gmail does with favorite emails in their inbox. Actually, it’s exactly like that.

  • 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-05-24T07:53:48+00:00Added an answer on May 24, 2026 at 7:53 am

    First you need to set up the database to handle this, personally I’d go with a has_many :through association because it provides more flexibility over has_and_belongs_to_many. The choice, however, is up to you. I recommend you look up the different types in the API and decide for yourself. This example will deal with has_many :through.

    Models

    # user.rb (model)
    has_many :favorites
    has_many :posts, :through => :favorites
    
    # post.rb (model)
    has_many :favorites
    has_many :users, :through => :favorites
    
    # favorite.rb (model)
    belongs_to :user
    belongs_to :post
    

    Controller

    # favorites_controller.rb
    def create
      current_user.favorites.create(:post_id => params[:post_id])
      render :layout => false
    end
    

    Routes

    match "favorites/:post_id" => "favorites#create", :as => :favorite
    

    jQuery

    $(".favorite").click(function() {
      var post_id = $(this).attr('id');
      $.ajax({
        type: "POST",
        url: 'favorites/' + post_id,
        success: function() {
          // change image or something
        }
      })
    })
    

    Notes

    This assumes a couple of things: Using Rails 3, using jQuery, each favorite icon has an html id with the post id. Keep in mind I’ve not tested the code and I wrote it in this window so you probably have to fix some minor problems, but it should give you an impression of how I usually does this. The visual stuff and such I’ll leave up to you.

    If anyone spot any mistakes please feel free to edit this post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables users registered_members I want to confirm values from user table
I would like to read about your different strategies. For example, if you have
I have two mysql queries: $sql = SELECT * FROM content WHERE threadName LIKE
I have these tables: USERS: user_ID username password email data_registered POSTS: post_ID user_ID post_title
I am working on a small project where a client would like to have
I have to queries: select id, name, num from pending id, name, num 1,
I have two queries, as following: SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name
i have some queries which group datasets and count them, e.g. SELECT COUNT(*) FROM
I have 2 queries. First: SELECT * FROM `table` WHERE col='xyz' LIMIT 100 //Time
I want to send only one variable via the post method. I have queried

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.