ActiveRecord :: AssociationTypeMismatch al intentar guardar atributos anidados en Rails
Leí muchas páginas sobre las relaciones `has_one y los atributos anidados, pero no he logrado que esto funcione. Cualquier ayuda sería fantástica
Cada usuario tiene una red. Estoy tratando de recopilar información para ambos atributos en una forma, pero sigo recibiendo la excepciónActiveRecord::AssociationTypeMismatch in UsersController#create
Los parámetros pasados son:
{"utf8"=>"✓",
"authenticity_token"=>"I54tm1ovzHEHaXbBLTT+5tqBJv2795sKg978ot3HDBc=",
"user"=>{"name"=>"Bilbo Baggins",
"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"network"=>{"home_lng"=>"-87.91894912719727",
"home_lat"=>"43.03812464542969",
"center_lng"=>"-87.91894912719727",
"center_lat"=>"43.03812464542969",
"radius"=>"500"}},
"commit"=>"Sign up"}
Estoy adivinando los parámetros paraNetwork
tiene que aparecer de alguna manera comonetwork_attributes
pero no estoy seguro de cómo.
Controlador
def create
@user = User.new(params[:user])
if (@user.save)
sign_in @user
flash[:success] = "One ring to rule them all!"
redirect_to @user
else
@title = "The journey begins..."
render 'new'
end
end
Ver
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %><br />
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.fields_for @network do |fn| %>
<%= fn.hidden_field :home_lng %>
<%= fn.hidden_field :home_lat %>
<%= fn.hidden_field :center_lng %>
<%= fn.hidden_field :center_lat %>
<%= fn.hidden_field :radius %>
<% end %>
y por supuesto, los Modelos:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :network_attributes, :network
has_one :network, :foreign_key => "user_id",
:dependent => :destroy
accepts_nested_attributes_for :network,
:reject_if => :all_blank,
:allow_destroy => true
end
class Network < ActiveRecord::Base
attr_accessible :home_lng, :home_lat, :center_lng, :center_lat, :radius
belongs_to :user
end
Gracia