I've been at this for hours now but can't find a solution and am now wondering if I'm approaching this correctly.
I have an employee form and in it I'm creating multiple ee_pay records (which store a reference to the company_pay type they are). The problem is accessing the company_pay associated with the ee_pay that I'm creating in the form.
The code below works but I keep getting the description of the second @taxable_pays type in the variable @description in _pay_types.html.erb
I'm not entirely sure why this is happening. I've tried multiple ways of passing the description of each one. I know there's two in that collection because the correct ids are getting stored in two separate ee_pay records that get saved when I submit the form.
Can anyone tell me how I can pass the value of each @taxable_pays.description to the view? And also tell me if I'm approaching this in the best way?
Thanks for looking. Here's my code:
employees_controller.rb
I have a before_action that does the following:
@taxable_pays = CompanyPay.where(company_id: current_company.id).order(:id)
Then:
def new
@taxable_pays.each do |pt|
pay_type = @employee.ee_pay.build(company_pay_id: pt.id)
@description = pt.description
end
_form.html.erb
<div class="row">
<div class="col-md-4">
<div class="form_label"><strong>Description</strong></div>
</div>
<div class="col-md-4">
<div class="form_label"><strong>Amount</strong></div>
</div>
<div class="col-md-4">
<div class="form_label"><strong>Rate</strong></div>
</div>
<%= f.fields_for :ee_pay do |builder| %>
<%= render "pay_types", :f => builder %>
<% end %>
</div>
_pay_types.html.erb
<div class="col-md-4">
<div class="form_indent1"><div class="form_indent1"><%= @description %></div></div>
<div class="form_spacer"></div>
</div>
<div class="col-md-4">
<div class="form_indent1"><%= f.text_field :amount, class: "number_input" %></div>
<div class="form_spacer"></div>
</div>
<div class="col-md-4">
<div class="form_indent1"><%= f.text_field :rate, class: "number_input" %></div>
<div class="form_spacer"></div><br />
</div>
Edit 1 (updated)
After advice from Mark G. below my code now looks like:-
employees_controller.rb
@pay_types = []
@taxable_pays.each do |pt|
@pay_types << @employee.ee_pay.build(company_pay_id: pt.id)
puts "Debug for the pay_types"
puts @pay_types
end
_form.html.erb
<div>
<% @pay_types.each do |ee_pay| %>
<%= f.fields_for :ee_pay do |builder| %>
<%= render "pay_types", :f => builder, :ee_pay => ee_pay %>
<% end %>
<% end %>
</div>
_pay_types.html.erb
<%= f.hidden_field :company_pay_id %> (not on original edit)
<div class="col-md-3">
<div class="form_indent1"><div class="form_indent1"><%= ee_pay.company_pay.description %></div></div>
<div class="form_spacer"></div>
</div>
<div class="col-md-3">
<div class="form_indent1"><%= f.text_field :amount, class: "number_input" %></div>
<div class="form_spacer"></div>
</div>
<div class="col-md-2">
<div class="form_indent1"><%= ee_pay.company_pay.units %></div>
<div class="form_spacer"></div>
</div>
<div class="col-md-4">
<div class="form_indent1"><%= f.text_field :rate, class: "number_input" %></div>
<div class="form_spacer"></div><br />
</div>
I originally thought this was each of the @pay_types printing twice but looking at it again it's the description printing twice, each time with each of the company_pay_ids
*description* *company_pay_id*
Basic 2
Basic 8
Time & 1/2 2
Time & 1/2 8
I'm really stumped with this.
Edit 2
This is the console output I'm getting when I try create a new employee
CompanyPay Load (0.7ms) SELECT "company_pays".* FROM "company_pays" WHERE "company_pays"."id" = $1 LIMIT 1 [["id", 2]]
Rendered employees/_pay_types.html.erb (2.8ms)
Debug in the _form view fields_for
#<EePay:0x007f4130a214a8>
Rendered employees/_pay_types.html.erb (0.5ms)
Debug in the _form view fields_for
#<EePay:0x007f4130a214a8>
CompanyPay Load (0.8ms) SELECT "company_pays".* FROM "company_pays" WHERE "company_pays"."id" = $1 LIMIT 1 [["id", 8]]
Rendered employees/_pay_types.html.erb (2.4ms)
Debug in the _form view fields_for
#<EePay:0x007f4130a1fcc0>
Rendered employees/_pay_types.html.erb (0.7ms)
Debug in the _form view fields_for
#<EePay:0x007f4130a1fcc0>
Rendered employees/_form.html.erb (37.5ms)
Rendered employees/new.html.erb within layouts/application (38.3ms)
I'm giving up for the night. Been looking at it for too long and getting nowhere. Thanks for the help. Will try again tomorrow
@pay_typescollection? Is that length 2 or 4? Your code looks syntactically correct, so I would start checking your variable values and see whats causing the extra iterations. You can uselogger.infoin your controller to do some primitive debugging.@pay_typesand in the terminal it's showing that there's 2 objects with different references#<EePay:0x007f41307346c8> #<EePay:0x007f4130732828>. I'm banging my head off the wall here. Just can't see the reason for rendering twice for each object in@pay_types. Would showing you the generated html be any use?fields_fortag and just try rendering the partial without the form fields, do you still get duplicates? If that still happens, try naming the partial something else and see if it still occurs?_pay_types.html.erbwith random text and removed thefields_forfrom_form.html.erb. The partial only rendered twice which is correct