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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:16:55+00:00 2026-05-15T11:16:55+00:00

I have a boolean field called saved in my database. I want to toggle

  • 0

I have a boolean field called “saved” in my database. I want to toggle this field by clicking on a text link that changes from “Save” to “Unsave” depending on the situation, and updates my “Customer” table with 0 or 1. I imagine Javascript may be a way to go for this but I am not experienced enough (yet!) in Javascript to know how to code it.

I’ve rolled back the question to keep it shorter. Here is my exact code.

#employers controller
def save_toggle
  @matching = Matching.find(params[:id])
  if @matching.employer_stars == false
    @matching.employer_rejects = false # If saving a match, remove any existing rejection.
  end
  @matching.employer_stars = !@matching.employer_stars
  @matching.save
  render :partial => "save_unsave_buttons", :layout => false
end

#view home.html.erb
<%= render :partial => "save_unsave_buttons", :locals => {:matching => matching} %>

#partial _save_unsave_buttons.html.erb
<div id="save_buttons" class="buttonText"> #latter is just for CSS layout
  <% if @matching.employer_stars %>
    <%= link_to_remote "Unsave",
      :url => {:action => "save_toggle", :id => matching.id},
      :update => {:success => "save_buttons", :failure => "Error"} %>
  <% else %>
    <%= link_to_remote "Save",
      :url => {:action => "save_toggle", :id => matching.id},
      :update => {:success => "save_buttons", :failure => "Error"} %>
  <% end %>
</div>

The database is working but the toggle text isn’t switching. To @nathanvda: I’m really sorry for being such a pain – I want to confirm your answer but I know if I do I’ll just leave this for a while then come back to it and get frustrated again! Thanks man.

  • 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-15T11:16:56+00:00Added an answer on May 15, 2026 at 11:16 am

    You have to define a controller-method, which sets your saved attribute. In your view you can then link to this method using link_to_remote.

    That should get you started.

    –Update: after updated question:

    You should create a partial that renders your save/unsave button like this, call it “_save_unsave_buttons.html.erb” :

    <div id="save_buttons">
      <% if matching.employer_stars %>
        <%= link_to_remote "Unsave",
           :url => {:action => "save_toggle", :id => matching.id},
           :update => {:success => "save_buttons", :failure => "Error"} %>
      <% else %>
        <%= link_to_remote "Save",
           :url => {:action => "save_toggle", :id => matching.id},
           :update => {:success => "save_buttons", :failure => "Error"} %>
      <% end %>
    </div>
    

    This partial will render you correct save-buttons, and upon update the containing div is updated/replaced by the result of your controller action.

    From inside your main view, write

    <%= render :partial => "save_unsave_buttons", :locals => {:matching => match } %>
    

    where you want the buttons to be visible.

    And inside your controller:

    def save_toggle
      @matching = Matching.find(params[:id])
      @matching.employer_stars = !@matching.employer_stars
      @matching.save
      render :partial => "save_unsave_buttons", :locals => {:matching => @matching}, :layout => false
    end
    

    Good luck!

    –Update again: so i presume you render a set of @matchings, i would change the naming between the collection and the item a bit, to prevent more confusion and accidental mistypings.

    But actually this is pretty easy:

    @matchings.each do |match|
      .. build your view here ..
      <%= render :partial => "save_unsave_buttons", :locals => {:matching => match}
    end
    

    and in your partial you can then use the correct matching everywhere.

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

Sidebar

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.