Rails 3:使用has_one设计,嵌套属性未更新 [英] Rails 3: Devise with has_one, Nested attributes not updated

查看:84
本文介绍了Rails 3:使用has_one设计,嵌套属性未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为用户名为user_info创建一对一的关系。
以下是工作解决方案。

I am creating a one-to-one relationship to user called user_info. Below is the working solution.

在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

在user_info.rb中

In user_info.rb

belongs_to :user
attr_accessible :first_name, :last_name

在设计/注册/ edit.html.erb

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.

应该是:
<%resource.build_user_info如果resource.user_info.nil? %>

这篇关于Rails 3:使用has_one设计,嵌套属性未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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