Possible Duplicate:
Associated model is not saving data when page is refreshed
I have a 1:1 association between User and Profile.
When I submit the new profile form, the data I’ve entered is displayed just fine (see screenshot: https://i.stack.imgur.com/ygA2y.png), but when I refresh it the data is instantly wiped.
Could anyone tell me what is causing this?
Here’s the submit form:
<%= form_for([@user, @user.build_profile]) do |f| %>
<div
class="field">
<%= f.label :first_name %><br />
<%=
f.text_field :first_name %>
</div>
<div
class="field">
<%= f.label :last_name %><br />
<%=
f.text_field :last_name %>
</div>
<div
class="field">
<%= f.label :picture %><br />
<%=
f.text_field :picture %>
</div>
<div class="field">
<%= f.radio_button(:sex, "male") %>
<%=
f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female")
%>
<%= f.label(:sex, "Female") %>
</div>
<div
class="actions">
<%= f.submit %>
</div>
<%
end %>
Here’s the users_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb
Here’s the profiles_controller: https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb
Basically, it’s failing because:
passing
@user.build_profileinstantiates a new profile each time.sidenote but I highly recommend you create the profile just after the user is created.
Look at my commit on your forked repository.