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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:43:11+00:00 2026-06-16T05:43:11+00:00

I have a rails app, which is constructed by three parts such as navigation

  • 0

I have a rails app, which is constructed by three parts such as navigation above, main content, and side menu.

I already implemented follow un-follow button in my main content.
It’s working with Ajax perfectly so far.

basically if I press follow button, follow action in users_controller.rb will be called and changes follow flag, and then it calls follow.js to re-render follow button as partial.
Beside that, in side menu, it has the number of people whom current_user is following.
This number also should be refreshed right after follow action was executed.

To refresh more than 2 partials at once. How can I archive this?

controllers/users_controller.rb

....

  def follow
    @user = User.find(params[:id])
    current_user.follow(@user)
    respond_to do |format|
      format.js {render :action=>"follow.js"}
    end
  end

  def unfollow
    @user = User.find(params[:id])
    current_user.stop_following(@user)
    respond_to do |format|
      format.js {render :action=>"unfollow.js"}
    end
  end

...

views/users/follow.js.erb

$('.follow_user[data-user-id="<%=@user.id%>"]').html('<%= escape_javascript(render :partial => "users/follow_user", :locals => {:user => @user}) %>');

views/users/unfollow.js.erb

$('.follow_user[data-user-id="<%=@user.id%>"]').html('<%= escape_javascript(render :partial => "users/follow_user", :locals => {:user => @user}) %>');

views/layouts/_menu.html.erb

          <% if user_signed_in? %>
                  <li><%= link_to(following_user_path(current_user.username)) do %>
                  <i class="icon-heart"></i>Following
                  <%= '(' + current_user.all_following.count.to_s + ')' if current_user.all_following.count > 0 %>
                  <% end %>
                  </li>    

                  <li><%= link_to(followed_user_path(current_user.username)) do %>
                  <i class="icon-heart"></i>Followed by
                  <%= '(' + current_user.followers.count.to_s + ')' if current_user.followers.count > 0 %>
                  <% end %>
                  </li>
              <% else %>
                  <li><%= link_to sanitize('<i class="icon-heart"></i> ') + "Following", following_user_path %></li>                                            
                  <li><%= link_to sanitize('<i class="icon-heart"></i> ') + "Followed by", followed_user_path %></li>                                           

              <% end %>
  • 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-16T05:43:13+00:00Added an answer on June 16, 2026 at 5:43 am
    # dedicate partial _following_number.html.erb
    <span id="following_number">
      <% if user.followers.count > 0 %>
        <%= '(' + user.followers.count.to_s + ')' %>
      <% end %>
    </span>
    
    
    # update layout with partial:
    
    <li><%= link_to(followed_user_path(current_user.username)) do %>
    <i class="icon-heart"></i>Followed by
      <%= render :partial => "following_number", :user => current_user %>
    <% end %>
    
    # follow.js.erb && unfollow.js.erb:
    ...
    $(document).find("#following_number").replaceWith('<%= escape_javascript(render("following_number", :user => @user, :formats => [:html])) %>')
    

    More elegant with helper:

    def following_number(user)
      user.followers.count > 0 ? "(#{user.followers.count})" : nil
    end
    
    # then put spans to layout:
    <span id="following_number"><%= following_number(current_user) %></span>
    
    # and js:
    $(document).find("#following_number").html('<%= escape_javascript(following_number(@user)) %>')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tab navigation page in my rails app which is shared across
I have a rails app which is already running with some users, users that
I have rails app which are working perfectly in the local computer. But when
So I have a Rails app (which in this case seems like it would
I have my rails app, which is filled with products, set up with a
I have a Rails 3.1.3 app which uses devise for users authentication and soft-deletes
I have a rails app running on passenger standalone, which is working perfectly. I
I have got Sinatra/Rails app and an action which starts some long process. Ordinary
Simple rails app: I have 2 models, user and intro [which is simply a
I have a JavaScript widget which communicates with my Rails app by creating tags

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.