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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:20:33+00:00 2026-06-02T21:20:33+00:00

I’m a total newbie when it comes to AJAX and need a little help.

  • 0

I’m a total newbie when it comes to AJAX and need a little help. I have a posts_controller and a likes_controller:

A user can ‘like’ or ‘unlike’ a post. Right now, I’m trying to implement AJAX so that the like/unlike will toggle whenever a user ‘likes’ a post or ‘unlikes’ a post.

Before AJAX the page would refresh, and the code in the view would determine whether or not a given post was liked by the current user. I ran an if statement in the view, and depending on the results, displayed two different forms–if the post had not yet been liked there was a form to ‘like’ the post (create action), and if the post was already liked, there was a form to ‘unlike’ the post (destroy action).

Now that I’ve implemented the create part using AJAX, the like happens dynamically but the view does not change (I can see that the like is created via terminal / rails console, but the text on the link remains the same). I would like to know what is the best way to update the view without refreshing the page? I need to update the ‘like’ link and form with the ‘unlike’ link and form once the like happens via AJAX. Is the only way to do this via jquery?

here is my code:
likes_controller

class LikesController < ApplicationController

  def create
    @post = Project.find(params[:like][:likee_id])

    respond_to do |format|
      format.html{
        current_profile.like!(@post)
        redirect_to :back
      }
      format.js {
        current_profile.like!(@post)
      }
    end  

  end
  def destroy
    @post = Like.find(params[:id]).likee
    current_profile.unlike!(@post)
    redirect_to :back
  end
end

a snippet of the view:

%ul.dropdown-menu
  %li
    - if current_profile.like?(post)
      = form_for current_profile.likes.find_by_likee_id(post), :html => { :method => :delete } do |f|
        %a{:href => "javascript:void(0)", :onclick=>"$(this).closest('form').submit()"}
          %i.icon-heart.icon-large{:style => "color: #fedc73;"}
          Unlike this Post
      - else
        = form_for current_profile.likes.build(:likee_id => post.id), :remote => true do |f|
          = f.hidden_field :likee_id
          %a{:href => "javascript:void(0)", :onclick=>"$(this).closest('form').submit()"}
            %i.icon-heart-empty.icon-large
            Like this Post

create.js.haml file:

:plain
  $(this).closest('form').submit()

Any help would be extremely appreciated. Thank you so much!

  • 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-02T21:20:41+00:00Added an answer on June 2, 2026 at 9:20 pm

    You could use

    $.post('url', data, function(returned_data){ $.('like_div').html(returned_data) })
    

    But instead of manually creating a link, you could generate one with rails:

    = link_to url_for(...), :remote => true
    

    This will work, even if js is disabled, adn you don’t need any form for this.

    - if current_profile.like?(post)
      = link_to url_for(:controller => :posts, :action => :unlike, :id => post.id), :remote => true
    - else
      = link_to url_for(:controller => :posts, :action => :like, :id => post.id), :remote => true
    

    And of course this action should respond to .js and to .html. In the later one you can render the whole view, and in the .js you can insert a snippet of js which changes the link div into this refreshed one. Do you need any more explanation?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.