开发者

Fields_for setting up form not working correcty with accepts_nested_attributes

I'm sure that this is just a lack of experience on my part so bear with me.

I have a model MenuItem that has a Price. Different item types (menu_items, products, events) can have prices.

I have set it up as follows:

class MenuItem < ActiveRecord::Base
   ...
   has_one :price, :as => :pricable
   accepts_nested_attributes_for :price
   attr_accessible :price_attributes
   ...
end

class Price < ActiveRecord::Base
   belongs_to :pricable, :polymorphic => true
end

The Price object has a price value which is a decimal(8,2) on Mysql5.

In my form:

<%= form_tag "/menus/save" do %>
...
<% menu_header_form.menu_items.each do |item| %>
   <div><%=item['header'] %></div>
   <%=text_field :menu_item, :header, :index=>item.id, :value=>item.header %>
   &l开发者_如何学Pythont;%=text_field :menu_item, :sort, :index=>item.id, :value=>item.sort, :size => 2 %>
   <% item.fields_for :price do |menu_item_price| %>
        <%= menu_item_price.text_field :price %>
   <% end %>

<% end %>

and am getting the following error:

undefined method `fields_for' for #<MenuItem:0x007fec8d9be138> 

How would I iterate through to get the price value? Would the way that my models are set up mean that those menu_items would have a price record associated with them by default(even empty / null values)?

thx


you need fields_for :price , not item.fields_for

for a more complete example, take a look at the pattern here

All menu items would have a null value for price unless you explicitly give them one which you could do in a before_save callback if you chose

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜