ruby on rails - Associated model is not saving data when page is refreshed -


rails 3.1 rc4

i have 1:1 association between user , profile. when submit new profile form, data i've entered displayed fine (see screenshot: http://i.imgur.com/fy8yu.png), when refresh data instantly wiped.

could tell me causing this?

here's submit form:
<%= form_for([@user, @user.build_profile]) |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 users_controller: https://github.com/imjp/supermodel/blob/master/app/controllers/users_controller.rb

here's profiles_controller: https://github.com/imjp/supermodel/blob/master/app/controllers/profiles_controller.rb

i'm not sure agree approach. why don't this:

in models/user.rb:

accepts_nested_attributes_for :profile 

in controllers/users_controller.rb:

def new   @user = user.new   @user.build_profile end 

in views/users/_form.html.erb:

<%= form_for @user |f| %>   <%= f.text_field :first_name %>   <%= f.fields_for :profile |pf| %>     <%= pf.text_field :some_profile_field %>   <% end -%> <%- end -%> 

this isn't copied or tested, should work. on saving user, profile fields sent along , validated user fields , re-rendered when rendering form again after save error. way keep full control on form , contents minimal effort.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -