I am creating a one-to-one relationship to user called user_info. Below is the 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 devise/registrations/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 build in the view. But I do not want to 'touch' the devise controller or model. This is the easiest way.
Turns out that the build line is not working properly in the view.
Should be:
<% resource.build_user_info if resource.user_info.nil? %>
Try :autosave => true
on your call to accepts_nested_attributes_for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With