Ive tried all the videos and articles and still could not find a solution to get the event_ fields_to select the whitelisted values in strong_params. I spent days trying to figure it out (and asked a number of people). If anyone could spend time helping, I would be very grateful!
I have a relationship between many and many between List and Topic, with List_Topic acting as a union model. Using form_for, I created a form for the (@list) List instance, and then field_for: list_topics. In the fields for which Ive created collection_select, which is populated by Topic.all.
<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 %>
In my list controller, I:
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
Parameters from the form for fields_for are passed as:
list_topics_attributes"=>{"0"=>{"content"=>"Hey", "id"=>["", "2"]}}}
strong_params @list, custom_attribute, , : fields_for through: list_topics_attributes, : id strong_params collection_select , , /, . .
git repo. /new
https://github.com/jwolfe890/Top5/blob/master/app/views/lists/new.html.erb
!