mardi 14 décembre 2021

Rails permit params not working for multiple checkboxes and multi select

Creating new entry for Associate model have book_id and tag_id. My requirement is to have a list of checkboxes of books and multi-select for tags, which I have done like that

<%= form.collection_check_boxes(:book_id, books_hash, :last, :first) do |b| %>
  <div class="form-check form-check-inline">
      <%= b.check_box class: 'form-check-input' %>
      <%= b.label class: 'form-check-label' %>
  </div>
<% end %>

<div class="field">
   <%= form.label :tag_id %>
   <%= form.select :tag_id, options_for_select(Tag.pluck(:title,:id)), {}, {:multiple => true, :class => "chzn-select" } %>
</div>

Permit params are like this

params.require(:book_tag).permit(book_id: [], tag_id: [])

But issue is that when I save the record it gives an error

ActiveRecord::RecordInvalid (Validation failed: Book must exist, Tag must exist):

I can't understand what exactly i am missing.




Aucun commentaire:

Enregistrer un commentaire