Проблема с ассоциацией на рельсах 3 - PullRequest
1 голос
/ 24 августа 2011

Вид

<%= 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)

Пожалуйста, помогите мне решить эту проблему

Ответы [ 2 ]

6 голосов
/ 24 августа 2011

Попробуйте, если добавление accepts_nested_attributes_for :address решит это.

0 голосов
/ 23 марта 2013
accepts_nested_attributes_for :address

если не работает, попробуйте также добавить вместе с acceptpts_nested_attributes_for: address

attr_accessor :address
...