Rails 3: development with has_one, nested attributes are not updated

I create a one-to-one relationship with user_info. The following is a working solution.

In user.rb

has_one :user_info
accepts_nested_attributes_for :user_info, :allow_destroy => true
attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :user_info_attributes

In user_info.rb

belongs_to :user
attr_accessible :first_name, :last_name

In development / registration / edit.html.erb

<% resource.build_user_info if resource.user_info.nil? %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
    ...
    <%= f.fields_for :user_info do |info| %>
        <%= info.text_field :first_name %>

I understand that I should not include the assembly in the view. But I do not want to "touch" the controller or device model. This is the easiest way.

+5
source share
2 answers

It turns out that the assembly line is not working properly in the view.

Must be: <% resource.build_user_info if resource.user_info.nil? %>

+3
source

Try :autosave => trueon callaccepts_nested_attributes_for

+1
source

All Articles