I have two models:
class PortStock < ApplicationRecord
has_many :closed_positions
accepts_nested_attributes_for :closed_positions
end
class ClosedPosition < ApplicationRecord
belongs_to :closer, class_name: "PortStock", foreign_key: "closer_id"
belongs_to :closed, class_name: "PortStock", foreign_key: "port_stock_id"
end
I know this association works, because I have tested it otherwise.
I then have a form partial at views/port_stocks/sell/_form.html.erb
that looks like this:
<%= simple_form_for @port_stock, url: port_stocks_sell_order_path, method: :post do |f| %>
<% @port_stocks.each do |port_stock| %>
<%= f.simple_fields_for :closed_positions, html: { class: "form-inline" } do |c| %>
<%= c.input_field :num_units, id: "sell-ps-#{port_stock.id}", class: "form-control mx-sm-3" %>
<% end %>
<% end %>
<%= f.button :submit, "Sell Stock", class:"btn btn-primary" %>
<% end %>
But, when the HTML is rendered, it doesn't render any of the text fields within the simple_fields_for
.
If I put a binding.pry
right before the f.simple_fields_for
line, it halts execution. If I put it right after, it doesn't. That tells me that the simple_fields_for
isn't being executed.
But per the docs that's how I should do it.
What am I missing? Why does this not render the form fields within the simple_fields_for
?
I figured it out.
I didn't actually build
the closed_positions
in my controller.
So once I added this code to my controller:
@port_stock.closed_positions.build
That worked like a charm.