4

私はすべてのビデオと記事を試しましたが、fields_for collection_select の値を strong_params のホワイトリストに入れるための解決策を見つけることができません。私はこれを理解しようと何日も費やしました(そして多くの人に尋ねました)。誰かが時間をかけて助けてくれたら、私は非常に感謝しています!

List_Topic が結合モデルとして機能し、List と Topic の間に多対多の関連付けがあります。form_for を使用して、List のインスタンス (@list) のフォームを作成し、次に fields_for :list_topics を作成しました。のフィールド内に、Topic.all によって入力される collection_select を作成しました。

<br>
 <%= form_for(@list) do |f| %>
  <%= f.label :subject %>
  <%= f.text_field :subject %>
<br>
<br>

 <%= f.fields_for :list_topics do |ff| %>
  <%= ff.label "Choose a Topic:"  %><br>
  <%= ff.label :content %>
  <%= ff.text_field :content %>
  <%= ff.collection_select(:id, @all_topics, :id, :name, {}, {multiple: true}) %>
 <% end %>

 <%= f.submit %>
<% end %>

私のLists Controllerには次のものがあります:

class ListsController < ApplicationController

  def new
    @list = List.new
    @all_topics = Topic.all
    @list.list_topics.build 
  end

  def create
    @list = List.new(list_params)
  end

private

  def list_params
    params.require(:list).permit(:subject, :list_topics_attributes =>    [:topic, :content, :topic_ids, :id, :ids])
  end  

end

fields_for のフォームからのパラメーターは、次のように渡されます。

list_topics_attributes"=>{"0"=>{"content"=>"Hey", "id"=>["", "2"]}}} 

strong_params は @list のホワイトリストに登録されており、fields_for の :content パラメーターを :list_topics_attributes で認識できるように作成した custom_attribute ライターを取得できますが、渡された strong_params の :id パラメーターをホワイトリストに登録できませんでした。私が何をしようとも、私がフォローしている記事/ビデオに関係なく、collection_select を介して入力します。それらは単に表示されません。

ここにも git リポジトリがあります。フォームはリスト/新規の下にあります

https://github.com/jwolfe890/Top5/blob/master/app/views/lists/new.html.erb

洞察力をどうもありがとう!

4

1 に答える 1