Ruby on Rails
I want to save multiple values ​​selected in collection_check_boxes using the data of ActiveHash model.

The purpose of the function is to register the seasonings at home as user data.
Since the seasoning data does not change frequently, I am trying to implement it using ActiveHash.

If i can save as defined by the create action without displaying an error message, you cannot save even if you return to the top page as conditional branch processing.

When I check the seasonings table (seasonings) of MySQL, the record is added with the user_id properly linked, but the value selected in collection_check_boxes is not saved in the salt_sugar_id column and is NULL.

What I tried

I stopped the crate action at binding.pry and checked the terminal log and found that there was a problem with the parameters in the salt_sugar_id column and tried the following:

  • Checked for typographical errors in all descriptions related to the salt_sugar_id column
  • While validation is numeric: {other_than: 1}, I guess it is because there is a blank "" value in salt_sugar_id, so I deleted the default part of the SaltSugar model self.data. Ta
  • Since I want to store multiple values, I thought that the association of belongs_to might be wrong, so I verified it with has_many, but I get NoMethodError.
  • I tried to make it a permit! method. The parameter error statement no longer appears in the terminal log, but it is a page transition when the value is not saved in MySQL and cannot be saved.

In addition to the information below, we will immediately supplement any necessary items such as the following items.
Even if it is not direct, I would be grateful if you could teach me just the ideas and hints.
Thank you.

Started POST "/ seasonings" for :: 1 at
Processing by SeasoningsController # create as HTML
  Parameters: {"authenticity_token" =>"HgwBaS0yU0S6Qf1kCr3IQ/k2X0OK9KlIxBz237TYrXLEjll3/02oSDL6qsJqGasb3XOUHcmhvX/ct8DegVOPPQ ==", "seasoning" =>{"su

 "commit" =>"register"}
Unpermitted parameter:: salt_sugar_id
  User Load (0.2ms) SELECT `users`. * FROM` users` WHERE `users`.` id` = 2 ORDER BY `users`.` id` ASC LIMIT 1
  ↳ app/controllers/seasonings_controller.rb: 25: in `seasoning_params'
   (0.2ms) BEGIN
  ↳ app/controllers/seasonings_controller.rb: 13: in `create'
  User Load (0.4ms) SELECT `users`. * FROM` users` WHERE `users`.` id` = 2 LIMIT 1
  ↳ app/controllers/seasonings_controller.rb: 13: in `create'
   (0.2ms) ROLLBACK
  ↳ app/controllers/seasonings_controller.rb: 13: in `create'
  Rendering seasonings/new.html.erb within layouts/application
  Rendered shared/_header.html.erb (Duration: 0.2ms | Allocations: 133)
  Rendered shared/_footer.html.erb (Duration: 0.0ms | Allocations: 5)
  Rendered seasonings/new.html.erb within layouts/application (Duration: 1.6ms | Allocations: 1372)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 53ms (Views: 23.3ms | ActiveRecord: 1.0ms | Allocations: 36486)
Corresponding source code

Only salt_sugar is excerpted from the column name, and the others are omitted.

Rails.application.routes.draw do
  devise_for: users
  root to:'seasonings # index'
  get'rakuten_search' =>'seasonings # index'
  resources: seasonings, only: [: index,: new,: create]
  resources: users, only: [: show]


class SeasoningsController</pre>
<pre><code data-language = "Ruby">class Seasoning</pre>
<pre><code data-language = "Ruby">class User</pre>
<p><code>app/models/salt_sugar.rb</code>ActiveHash model</p>
<pre><code data-language = "Ruby">class SaltSugar</pre>
<pre><code data-language = "Ruby"><% = render'shared/header'%>                 <% = link_to "uchino_aji", "/", class:'main-subtitle'%><br />       Our seasoning registration       <% = form_with model: @seasoning, url: seasonings_path, method:: post, local: true do | f |%>                  <span class ='optional'>(optional)</span>        <% = f.label "salt/sugar", class:'form-label'%><br />        <% = f.collection_check_boxes (: salt_sugar_id, SaltSugar.all,: id,: name, {},

 {class:'check_box', id:'salt-sugar'})%>                            <% = f.submit "submit", class:'recipe-detail-btn'%>              <% end%>        <% = render'shared/footer'%>


class CreateSeasonings
Supplementary information (FW/tool version, etc.)

GitHub (The error part has not been reflected)
rails 6.0.0
mysql2 0.5.3

  • Answer # 1

    Since there is the following description in the log, params [: salt_sugar_id] is played.

    Unpermitted parameter:: salt_sugar_id

    If you do the following,: salt_sugar_id will pass.

    params.require (: seasoning) .permit (: salt_sugar_id =>[]). merge (user_id: current_user.id)

    However, I don't think you can save the array in the salt_sugar_id column of seasoning.

    If you want to create as many seasonings as the number of selected salt_sugar_ids,
    Loop through salt_sugar_id to create seasoning.

    If there is one seasoning and there are multiple salt_sugars associated with it (seasoning has_many: salt_sugars)
    You need to create an intermediate table.