質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1881閲覧

hidden field multiple:true を使用して、中間テーブルに保存したい

tach1993

総合スコア1

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/07/12 10:13

編集2020/07/12 10:20

themaモデルを登録する際に、themaを構成するsort_objectのidを動的に入れるように組んでいます。
fields_forを使用して双方の中間テーブルであるsort_object_themasに登録させようとしています。

その際のデータベース保存ができません。

unpermitted parameterの影響か、
sort_object_themaモデルにthema_idは入るのですが、sort_object_idが入りません。

hidden fieldに配列で値を入れていることは何か影響があるのでしょうか?
配列でパラメータが送信できてるので問題なさそうですが、
やりたいことをタイトルに書かせていただいております。

下記のソースから、何かヒントをご教示いただけませんでしょうか。
よろしくお願いします。

Processing by Front::ThemasController#create as HTML Parameters: {"authenticity_token"=>"hZsZlExd/95LIjFpLR8RJrtJt0BE3ZwA2eA5ZTvfTpEw6G8elH43FYzvELOYe6aGlsNx0Qv3hQF+V/2uYt4KFg==", "thema"=>{"title"=>"a", "detail"=>"a", "sort_object_themas_attributes"=>{"0"=>{"sort_object_id"=>["2"]}}}, "commit"=>"Create Thema"} Unpermitted parameter: :sort_object_id (0.5ms) BEGIN ↳ app/controllers/front/themas_controller.rb:9:in `create' Thema Create (2.5ms) INSERT INTO `themas` (`title`, `detail`) VALUES ('a', 'a') ↳ app/controllers/front/themas_controller.rb:9:in `create' SortObjectThema Create (1.4ms) INSERT INTO `sort_object_themas` (`thema_id`) VALUES (67) ↳ app/controllers/front/themas_controller.rb:9:in `create' (5.8ms) COMMIT ↳ app/controllers/front/themas_controller.rb:9:in `create' No template found for Front::ThemasController#create, rendering head :no_content Completed 204 No Content in 75ms (ActiveRecord: 10.2ms | Allocations: 4291)

view

1 = form_for @thema do |f| 2 = f.label :title 3 = f.text_field :title 4 = f.label :detail 5 = f.text_area :detail 6 = f.label :category_id 7 = f.fields_for :sort_object_themas do |sot| 8 = sot.hidden_field :sort_object_id, multiple: true #valueにはjsで配列の値を挿入 9 = f.submit

controller

1class Front::ThemasController < ApplicationController 2 def new 3 @thema = Thema.new 4 @thema.sort_object_themas.build 5 end 6 7 def create 8 @thema = Thema.new(thema_parameter) 9 @thema.save 10 # redirect_to new_sort_object_path 11 end 12 13 private 14 def thema_parameter 15 params.require(:thema).permit(:title, :detail, :category_id, sort_object_themas_attributes: [:sort_pbject_id]) 16 end 17end

Thema

1class Thema < ActiveRecord::Base 2 belongs_to :category, optional: true 3 has_many :sort_object_themas 4 has_many :sort_objects, through: :sort_object_themas 5 accepts_nested_attributes_for :sort_object_themas 6end

SortObjectThema

1class SortObjectThema < ApplicationRecord 2 belongs_to :sort_object, optional: true 3 belongs_to :thema, optional: true 4end

SortObject

1class SortObject < ActiveRecord::Base 2 has_many :sort_object_themas 3 has_many :themas, through: :sort_object_themas 4end

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

fields_forを特に使用する必要はなかったようです。

中間テーブルに保存するだけなら、
該当箇所を

view

1 = f.hidden_field :sort_object_ids, multiple: true

controller

1class Front::ThemasController < ApplicationController 2 def new 3 @thema = Thema.new 4 # @thema.sort_object_themas.build 中間テーブルのビルドも不要 5 end 6 7 def create 8 @thema = Thema.new(thema_parameter) 9 @thema.save 10 redirect_to new_sort_object_path 11 end 12 13 def index 14 @themas = Thema.all 15 end 16 17 def get_objects 18 @objects = SortObject.search_by_kana_name(params[:name]) 19 render 'new', formats: :json, handlers: 'jbuilder' 20 end 21 22 private 23 def thema_parameter 24 params.require(:thema).permit(:title, :detail, :category_id, {sort_object_ids: []}) #これだけで中間テーブルへの保存が可能 25 end 26end

参考記事:

https://qiita.com/gctsubo/items/dcf60559142f2e8e580f

投稿2020/07/12 10:34

tach1993

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問