I have 2 models, clients and client_prices, I would like to have 2 client prices nested forms sections in the client’s show page. 1 nested form would be for non-custom prices and another for custom prices. The non-custom (custom == false) prices would only have the “custom” attribute available to edit. The “Custom Prices” would have all of the attributes available to edit.
I’ve tried a few different approaches, but I don’t know where to put the conditional logic to make this work correctly. I’m using simple_form to generate the forms, but I’m not married to it.
Controllers
class Client < ActiveRecord::Base
attr_accessible :name, :client_prices_attributes
has_many :client_prices
accepts_nested_attributes_for :client_prices, :allow_destroy => true
end
class ClientPrice < ActiveRecord::Base
attr_accessible :client_id, :price, :visit_type, :id, :custom
belongs_to :client
belongs_to :default_price
end
Client Show page
<%= simple_nested_form_for @client do |f| %>
<%= f.fields_for :client_prices do |def_price_form| %>
non-custom prices form here
<%end%>
<%= f.fields_for :client_prices do |def_price_form| %>
custom prices form here
<%end%>
<%end%>
Try the following: