I have a rails app in which I have a Project
model, a Team
model and a User
model. I am trying to create a nested form within the Project
form using the Team
model, and in this nested form I have a collection_select
field which allows the user to select an existing User's email (from the User
model) and input the data retrieved in a column called :member
within the Team
model. So far this is working fine, however the issue im having is that on the show page, the data collected from the collection_select
field is appearing as numbers (the user's ID) instead of the emails that were selected when submitting the form. Why is this happening and how can I fix the issue? Thanks in advance!
project form :-
<%= bootstrap_nested_form_for(@project, :html => {:multipart => true}, layout: :horizontal) do |f| %>
.
.
<% f.fields_for :teams do |builder| %>
<%= builder.collection_select :member, User.all, :id, :email, { prompt: "Please select", :selected => params[:user], label: "Employee" } %>
<%= builder.link_to_remove "Remove" %>
<% end %>
<%= f.link_to_add "Add Team Member", :teams %>
<%= f.submit %>
<% end %>
Show page:-
<p>
<strong>Team:</strong>
<% @project.teams.each do |team| %>
<%= team.member %>
<% end %>
</p>
</div>
</div>
According to this http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
You will check that third option is the value_method which is saved in table as value. So currently that is id
. You need to replace that with email
.
This will work
<%= builder.collection_select :member, User.all, :email, :email, { prompt: "Please select", :selected => params[:user], label: "Employee" } %>