I have 2 models:
class Store < ActiveRecord::Base
attr_accessible :prices_attributes, :business_name
has_many :prices
accepts_nested_attributes_for :prices
end
class Price < ActiveRecord::Base
attr_accessible :price, :product_name, :purchase_date
belongs_to :store
end
I’m creating a Store and Prices together making it a nested form:
class StoresController < ApplicationController
def new
@store = Store.new
3.times {@store.prices.build }
end
end
Nested form:
<%= form_for @store do |f| %>
<%= f.text_field :business_name %>
<%= date_select("price", "purchase_date") %>
<%= f.fields_for :prices do |up| %>
<%= up.text_field :product_name %>
<%= up.text_field :price %>
<% end %>
<% end %>
I want to place the purchase_date outside of my f.fields_for so users only have to choose one date_select for all prices made. This doesn’t work though. The purchase_date does not show up on the form. How can I get it do this?
If the child forms display the purchased_date and purchased inputs – and you want to set the values of those inputs based on the input values in the parent form – you’re going to have to script that on the client side (javascript/jquery) so the children’s values get set in response to the parent input’s change event (or similar).
If the child forms do not show these inputs, you will have to set the children’s attributes on the server side (model or controller) based on the parent’s attributes (if you choose to add them to the parent) or by using the params sent on the request.