刷新页面时关联模型不保存数据 [英] Associated model is not saving data when page is refreshed

查看:45
本文介绍了刷新页面时关联模型不保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rails 3.1 RC4

我在用户个人资料之间建立了一对一的关联.当我提交新的个人资料表单时,我输入的数据显示得很好(见截图:http://i.imgur.com/fY8YU.png),但是当我刷新它时,数据会立即被擦除.

谁能告诉我这是什么原因造成的?

这是提交表单:
<%= form_for([@user, @user.build_profile]) do |f|%>


<%= f.label :first_name %>

<%= f.text_field :first_name %>



<%= f.label :last_name %>

<%= f.text_field :last_name %>



<%= f.label :picture %>

<%= f.text_field :picture %>



<%= f.radio_button(:sex, "male") %>
<%= f.label(:sex, "Male") %>
<%= f.radio_button(:sex, "female") %>
<%= f.label(:sex, "Female") %>



<%= f.submit %>


<% end %>

这是 users_controller:https://github.com/imjp/SuperModel/blob/master/app/controllers/users_controller.rb

这是profiles_controller:https://github.com/imjp/SuperModel/blob/master/app/controllers/profiles_controller.rb

解决方案

我不确定我是否同意您的方法.你为什么不做这样的事情:

在模型/user.rb:

accepts_nested_attributes_for :profile

在控制器/users_controller.rb 中:

def new@user = User.new@user.build_profile结尾

在 views/users/_form.html.erb 中:

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

这不是复制或测试,但它应该工作.在保存您的用户时,配置文件字段将与用户字段一起发送并进行验证,并在保存错误后再次呈现表单时重新呈现.通过这种方式,您可以毫不费力地完全控制您的表单及其内容.

Rails 3.1 RC4

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: http://i.imgur.com/fY8YU.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

解决方案

I'm not sure I agree with your approach. Why don't you do something like 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 do |f| %>
  <%= f.text_field :first_name %>
  <%= f.fields_for :profile do |pf| %>
    <%= pf.text_field :some_profile_field %>
  <% end -%>
<%- end -%>

This isn't copied or tested, but it should work. On saving your user, the profile fields are sent along and validated with the user fields and are re-rendered when rendering the form again after a save error. This way you will keep full control over your form and its contents with minimal effort.

这篇关于刷新页面时关联模型不保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆