2011-08-24 97 views
1

查看問題與協會在軌道3

<%= form_for @product, :url => admin_products_path do |f| %> 
<div> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
</div> 

<div> 
    <%= f.label :description %> 
    <%= f.text_area :description, :rows => 7 %> 
</div> 
<%= f.fields_for :address do |a| %> 
    <p><%= a.label "Address One" %><br /> 
    <%= a.text_field :address_one %></p> 

    <p><%= a.label "Address Two" %><br /> 
    <%= a.text_field :address_two %></p> 

    <p><%= a.label :city %><br /> 
    <%= a.text_field :city %></p> 

    <p><%= a.label :postcode %><br /> 
    <%= a.text_field :postcode %></p> 

    <p><%= a.label :country %><br /> 
    <%= a.select :country_id, Country.active.collect {|p| [ p.printable_name, p.id ] } %></p> 
<% end %> 
<% end %> 

控制器

def create 
    @product = Product.new(params[:product]) 
    if @product.save 
     flash[:notice] = 'Product was successfully created.' 
     render 'show' 
    else 
     render 'new' 
    end 
    end 

模式

class Product < ActiveRecord::Base 
    has_one :address, :as => :addressable  
end 

但是當保存數據它顯示問題:
Address(#93165130) expected,got ActiveSupport::HashWithIndifferentAccess(#79365750)

請幫我解決了這個問題

回答

6

嘗試,如果添加accepts_nested_attributes_for :address解決這個問題。

+1

你也可能需要添加'attr_accessible:address_attributes'到'Product'模型。 –

+0

謝謝。它爲我工作。 – khanh

0
accepts_nested_attributes_for :address 

如果不工作,那麼嘗試也accepts_nested_attributes_for一起加入:地址

attr_accessor :address