Selectable check box area

Currently, my rails are marked only if you check the box; nothing happens if I click on the text associated with this flag. Is there a way to check the box to switch if you also press TEXT?

<% @books.each do |b| %>
  <%= check_box_tag "books[]", b.book %><%= b.book %><br />
<% end %>
+3
source share
2 answers

This was difficult due to [], which are needed to work with flag collections. Just follow these steps:

View:

<% @books.each do |b| %>
  <%= check_box_tag "books[#{b.id}]", b.book %>
  <%= label_tag "books[#{b.id}]", b.book %>
  <br />
<% end %>

Then in the controller access to the parameter will be values. Otherwise, it looks like 135 => Book1. Use valuesto get only Book1

PARAMS [: books] .values

- check_box_tag <label> :

...
<label><%= check_box_tag "books[]", b.book %></label>
...

, #{b.id},

params[:books]  # notice the .values is removed
+10
+4

Source: https://habr.com/ru/post/1782573/


All Articles