lundi 15 avril 2019

Ruby on Rails: Invert checkbox check/uncheck value on submit

I have a website, where you can register for some events I called talks. After the registration the "guest" gets a confirmation email with a generated unsubscribe link. The link opens a form with all the events listed that the guest registered for.

Now to unsubscribe from an event the guest has to uncheck the checkbox of the event. But I want it inverted so that they have to check the event to unsubscribe from it.


The models

class Guest < ApplicationRecord
  has_and_belongs_to_may :talks
end

class Talk < ApplicationRecord
  has_and_belongs_to_may :guests
end


The unsubscribe form

<%= form_for @guest do |f| %>
  <% f.collection_check_boxes :talk_ids, @guest.talks, :id, :name do |b| %>
    <%= b.check_box %>
    <%= b.object.name %>
  <% end %>

<input type="submit" value="Unsubscribe">
<% end %>


The update function after submitting

def update_talks
  @guest = Guest.find(params[:id])
  if @guest.update(guest_params)
    redirect_to root_url
  else
    flash[:notice] = "Failed"
  end
end

I couldn't find a solution so I hope you guys can tell me how to invert the checkbox?




Aucun commentaire:

Enregistrer un commentaire