0

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>
  • sorry i accidently removed the comment. but the link is http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select and this will work. `<%= builder.collection_select :member, User.all, :email, :email, { prompt: "Please select", :selected => params[:user], label: "Employee" } %>` i think – Athar Aug 04 '15 at 07:00
  • it worked thanks alot! :D – Rashed Al-Julaibi Aug 04 '15 at 07:03

1 Answers1

1

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" } %>
Athar
  • 3,258
  • 1
  • 11
  • 16
  • I posted another question just now relating to the same part of the application but with a slightly different problem. If it not too much trouble would you mind checking it out? http://stackoverflow.com/questions/31807042/rails-nested-form-data-not-appearing-in-edit-page-despite-showing-in-show – Rashed Al-Julaibi Aug 04 '15 at 10:40