Foosaurus
Foosaurus

Reputation: 49

Issue with parameters when using fields_for

So, I'm having an issue in rails where not all parameters are being passed to the controller upon update. This is has_many through relationship and I'm trying to update the join table.

The first model looks like this:

class UserProfile < ApplicationRecord
  belongs_to :user
  belongs_to :profession
  has_many :competencies, dependent: :destroy
  has_many :procedure_categories, through: :profession
  has_many :procedures, through: :competencies

  accepts_nested_attributes_for :procedures
  # required so that compentencies controller can save to the join table
  accepts_nested_attributes_for :competencies

The join model:

class Competency < ApplicationRecord
  belongs_to :user_profile
  belongs_to :procedure
end

My form:

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <p>Define your competencies details here.</p>
    <%= form_for @user_profile do |user_profile_form| %>

      <% @user_profile.procedure_categories.each do |procedure_category| %>
        <h2><%= procedure_category.category %></h2>

          <%= user_profile_form.fields_for :competency do |cf| %> 
              <% competencies = @user_profile.competencies.where(procedure_id: procedure_category.procedures)
                if competencies.count > 0
              %>
                  <table class='table'>
                    <thead>
                      <tr>
                        <th>Competency</th>
                        <th>Years experience</th>
                        <th>Time it takes to complete</th>
                        <th>Do you want to do this?</th>
                      </tr>
                    </thead>
                    <tbody class="categories">
                      <% competencies.each do |competency| %>
                        <tr>
                          <td><%= competency.id %></td>
                          <td><%= cf.text_field :time, id: :competency_id, class: 'form-control' %></td>
                          <td><%= cf.text_field :rating, id: :competency_id, class: 'form-control' %></td>
                          <td>TBD</td>
                        </tr>
                      <% end %>
                    </tbody>
                  </table>

My parameters:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"onUmLRzPiJZmdSvd5PvG4zlskkn2I7FEfy7EgigcCHFFrC8/txI5a+wz6KTklFX80DGKNkZ/+7RYxPdCS8+nTA==", "user_profile"=>{"competency"=>{"time"=>"500", "rating"=>"500"}}, "commit"=>"Save competencies", "id"=>"11"}

When I inspect the HTML, I see this:

<tbody class="categories">
   <tr>
      <td>17</td>
      <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
      <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
      <td></td>
   </tr>
   <tr>
       <td>19</td>
       <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
       <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
       <td></td>
   </tr>
   <tr>
       <td>20</td>
       <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][time]" /></td>
       <td><input id="competency_id" class="form-control" type="text" name="user_profile[competency][rating]" /></td>
       <td></td>
    </tr>
</tbody>

So from the generated HTML, I think that the issue is that all of my parameters look the same. I need to differentiate them somehow.

Thank you for your help!

Upvotes: 1

Views: 27

Answers (1)

Foosaurus
Foosaurus

Reputation: 49

So, if I look at it long enough, I'll figure it out. The issue was mostly with my fields_for statement. Here is all of the form code that got the job done:

<div class="col-md-6 col-md-offset-3">
  <p>Define your competencies details here.</p>
  <%= form_for @user_profile do |user_profile_form| %>  
    <% @user_profile.procedure_categories.each do |procedure_category| %>
      <h2><%= procedure_category.category %></h2>
      <table class='table'>
         <thead>
           <tr>
             <th>Competency</th>
             <th>Years experience</th>
             <th>Time it takes to complete</th>
             <th>Do you want to do this?</th>
           </tr>
           </thead>
           <tbody class="categories">
             <%= user_profile_form.fields_for :competencies, @user_profile.competencies.where(procedure_id: procedure_category.procedures) do |cf| %>
                 <tr>
                   <td><%= cf.object.id %></td>
                   <td><%= cf.text_field :time, class: 'form-control' %></td>
                   <td><%= cf.text_field :rating, class: 'form-control' %></td>
                   <td>TBD</td>
                 </tr>
               <% end %>
             </tbody>
           </table>   
         <% end %>
       <%= user_profile_form.submit "Save competencies", class: "btn btn-primary" %>
     <% end %>
   </div>

Upvotes: 0

Related Questions