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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:53:49+00:00 2026-05-23T09:53:49+00:00

I have a product, user, and like model. A user can like a product.

  • 0

I have a product, user, and like model. A user can like a product. I am trying to implement a simple like button which, upon a click, allows a user to like a product. Then the like button transforms into an unlike button, allowing the user to unlike a product. Pretty straightforward.

I have implemented the model/controller logic for the above. I am not very good with javascript/ajax and was wondering what the best way would be to implement the above functionality. I want the like/unlike actions to be handled via ajax. I am using rails 3 and jquery ujs for my app.

Thanks.

  • 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-23T09:53:49+00:00Added an answer on May 23, 2026 at 9:53 am

    My answer to your question is long, so I wrote up an example application. Here’s a snippet:

    There are many ways to skin this cat, but I like to render a partial and a single ujs template.

    _like_button.html.erb:

    <% if like = current_user.likes.find_by_product_id(@product.id) %>
      <%= form_for like, :html => { :method => :delete },
                         :remote => true do |f| %>
        <%= f.submit "Unlike" %>
      <% end %>
    <% else %>
      <%= form_for current_user.likes.build(:product_id => @product.id), :remote => true do |f| %>
        <%= f.hidden_field :product_id %>
        <%= f.hidden_field :user_id %>
        <%= f.submit "Like" %>
      <% end %>
    <% end %>
    

    toggle.js.erb, where "#like" is the div enclosing the form:

    $("#like").html("<%= escape_javascript render('like_button') %>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ArtistProduct model. User enters the product details, user can view the entered
I have a product registration form that allows the user to add additional product
I have a website which sells a product to the user (downloadable). I am
I have a field 'Description' which can have product descriptions with any unicode characters.
I have an order model that belongs_to :user , a product model, a join
I have a Product edit screen. The user can select a Vendor for the
I have the following relation in my model: A user can have several consumers
I have a product setup executable that copies some files to the user's hard
I have Product model and it has many categories with a has_many :through association
I have a product, X, which we deliver to a client, C every month,

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.