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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:56:34+00:00 2026-06-05T14:56:34+00:00

Basically I have a follow button and when click the page refreshes and I

  • 0

Basically I have a follow button and when click the page refreshes and I show an unfollow button in place. Below is the code I use to render the particular form needed:

follow_forms partial:

<% unless current_user?(@user) %>
  <% if current_user.following?(@user) %>
    <%= render 'relationships/partials/unfollow' %>
  <% else %>
    <%= render 'relationships/partials/follow' %>
  <% end %>
<% end %>

Any I changed the form to an ajax form because I don’t want the page refresh and on success of the form submission I’d like to replace the follow button/form with an unfollow button/form. This isn’t straight forward because only 1 form shows at a time so I can’t use my jquery selector to find this form anyway.

What I decided to do was create a new action that renders the follow_form partial this way the appropriate form will be available for me to manipulate with my jquery selector.

The new action:

class RelationshipsController < ApplicationController

  def get_follow_form
    respond_to do |format|
      format.html { render :partial => 'relationships/partials/follow_form_ajax' }
    end
  end
end

The problem now is that I don’t have access to the @user instance variable. That doesn’t matter to much because I can get the user who was just followed via the jquery success data then pass that as data in the new ajax call to get_follow_form_url and then pass that info into the partial as a local variable.

I still have an issue with the @user instance variable not being available. Which brings me to my question.

How can I make another value be used if the instance variable isn’t nil/doesn’t exist?

The form for following:

<%= form_for current_user.relationships.build(:followed_id => @user.id), :remote => true do |f| %>
<%= f.hidden_field :followed_id %>
<%= f.submit "Follow", :class => 'followButton' %>
<% end %>

Can I do something like this

 :followed_id => @user.id <-if this doesn't exist use this-> user.id 

There are other ways around this like creating new partials that are only used for this whole situation or creating some messy if statements but I feel like creating duplicate forms should be my very very very last option.

I look forward to you solutions thanks

Kind regards

  • 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-05T14:56:36+00:00Added an answer on June 5, 2026 at 2:56 pm

    There’s a very simple way to do this, assuming you have your ‘fallback’ ID:

    :followed_id => @user.present? ? @user.id : fallback_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a jQuery code for 4 buttons. Basically you click the button,
This is a follow up from my previous question I have this code basically
Basically each entity would have a collection of feeding behaviors that it would follow.
This is a follow-up on this question . I basically have the same question
We have a function header format that we have to follow. It basically looks
Basically I have like 2 images, and I want to show one for 3
I am trying to add a button to my WordPress template. Basically, I have
I basically have a program that filters records from one excel file to another
I basically have three tables, posts, images and postimages (this simply contains the ids
I basically have a unix process running and it is doing some heavy processing

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.