I am building a job app with rails. In creating a new company, a user has the ability to select perks associated with the company in the "Company new page" through checkboxes. Ths works well until a user tries to update/edit the perks associated with a company.
When a user tries to edit the perks, the edit method completely removes existing values even though the user did not uncheck them in the edit page. So for example, a company has perks such as pension and free lunch. The user decides to edit this and add health insurance to the already existing perks. The edit method in this instance deletes pension and free lunch and only save health insurance despite the user not unchecking pension and free lunch. What i want is for he edit/update method to save the new checked values in addition to the already existing ones provided they were not unchecked. Below is a sample code in the edit page
<% Perk.all.each do |perk| %>
<input type="checkbox", class="hidden", value="<%= perk.id %>"
name="company[perk_ids][]" id="company_perk_ids_<%= perk.id %>"/>
<label class="category-choice <%= "active" if
@company.perk_ids.include? perk.id %>" for="company_perk_ids_<%=
perk.id %>">
<% perk.name %>
</label
<% end %>
This is the sample code in my companies controller
def edit
@company = Company.find(params[:id])
end
def update
edit
@company.update(company_params)
if @company.update(company_params)
flash[:notice] = "Company profile successfully updated."
redirect_to @company
else
render :edit
end
end
And in my js, I have
$(document).ready(function(){
$(".category-choice").click(function(){
$(this).toggleClass("active");
});
});
Aucun commentaire:
Enregistrer un commentaire