I used multiple checkboxes while implementing the user registration function of the original app! I will post it for the first time as a memorandum for beginners! If you make a mistake, please point it out in the comments lol
<%= f.check_box :food, {multiple: true}, food, nil %>
It is like this. In the food part, enter the object name you want to specify. By the way, the column type is specified by text type. Write {multiple: true} to allow multiple selections.
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, food:[]])
end
Don't forget [].
It is like this.
<div class="field">
<% %w(Steak hamburger sushi omelet rice).each.with_index do |food,i| %>
<label>
<%= f.check_box :food, {multiple: true}, food, nil %>
<%= food %>
</label>
<% end %>
</div>
Checkbox items are placed in the array using% notation. Then, using each.with_index, the elements of the array are stored in the variable food and numbered. Don't forget to associate form items with the lebel tag
that's all! !! form is difficult lol Since I'm a beginner, I didn't know how to write it, and there were some things I couldn't understand, but I think it would be useful to be able to use it! If you make a mistake or have a better way to write it, please let me know! I tried to imitate it by referring to other sites, but I think the implementation was successful! The site I referred to is here! ⇓⇓ https://www.sejuku.net/blog/27132
Recommended Posts