[RUBY] Use multiple checkboxes in Rails6!

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

How to write a check box There are many ways to write a check box. This time I used the check_box method because there is a related model.
 <%= 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.

Allow key parameters Since devise is used for user registration in this application production, we will describe it in application_controller.rb to allow the parameter of the check box in the private method.
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, food:[]])
  end

Don't forget [].

How to write the form screen

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

Use multiple checkboxes in Rails6!
Use images in Rails
Use multiple databases with Rails 6.0
[Rails] Keyword search in multiple tables
[Rails] Use cookies in API mode
How to use custom helpers in rails
Group_by in Rails
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] Use jQuery
[Rails] How to use select boxes in Ransack
How to use JQuery in js.erb of Rails6
[Rails] How to use PostgreSQL in Vagrant environment
Use pagy for pagination in your Rails app.
Adding columns in Rails
Use java.time in Jackson
Disable turbolinks in Rails
CSRF measures in Rails
Use Interceptor in Spring
Use OpenCV in Java
^, $ in Rails regular expression
Use MouseListener in Processing
Use PostgreSQL in Scala
Understand migration in rails
Use PreparedStatement in Java
Split routes.rb in Rails6
Implement markdown in Rails
Use Extend (Concerns) in Rails to standardize Controller processing.
I want to use a little icon in Rails
Implement user follow function in Rails (I use Ajax) ②
Beginners use ubuntu in windows to prepare rails environment
Get UserAgent in [Rails] controller
Implement application function in Rails
Declarative transaction in Rails #ginzarb
Implement follow function in Rails
[Rails] How to use enum
[RSpec] Let's use FactoryBot [Rails]
Japaneseize using i18n with Rails
Implement LTI authentication in Rails
Use ruby variables in javascript.
Error in rails db: migrate
How to use rails join
Gem often used in Rails
Display Flash messages in Rails
Use devise on multiple models
Establish a search bar in Rails ~ Join multiple tables to search
View monthly calendar in Rails
Implement import process in Rails
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
Rewrite Routes in Rails Engine
Use your own classes in the lib directory with Rails6
[Docker] Use whenever with Docker + Rails
[Rails] How to use Scope
Precautions when using checkboxes in Thymeleaf
[Rails] How to use gem "devise"
How to use Lombok in Spring
Use voice recognition function in Unity
Rails: Capture regular expressions in emails!
[Rails] How to use devise (Note)
[Rails] How to use flash messages