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 = [email protected]_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.