简体   繁体   English

无法更新has_one关联的嵌套模型表单

[英]Can't update my nested model form for has_one association

I try to create a nested model form for the has_one association. 我尝试为has_one关联创建一个嵌套的模型表单。 (i'm using Rails 4) (我正在使用Rails 4)

In my user, and adress model i have the following : 在我的用户和地址模型中,我有以下内容:

class User < ActiveRecord::Base
 has_one :address
 accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
 belongs_to :user

end

my user controller : 我的用户控制器:

class UsersController < ApplicationController
    .
    .
    .
    def edit
      @user = User.find(params[:id]) 
      @user.build_address if @user.address.nil?
    end 

    def update
      @user = User.find(params[:id])
      if @user.update(params.require(:user).permit(:user_name, address_attributes: [:street]))
        flash[:success] = "Profile updated successfully"
        sign_in @user
        redirect_to @user
      else
         flash.now[:error] = "Cannot updating your profile"
         render 'edit'
      end
    end
end

finally in my view i have : 终于在我看来我有:

= form_for(@user) do |f|
  = render 'shared/error_messages', object: f.object
  %div
    = f.label :user_name, "User name"
    = f.text_field :user_name
    = f.fields_for :address do |add|
      = addd.label :street
      = d.text_field :street
    = f.submit "Update"

When i try to fill street filed for the first time it works, but when i try to update i get the error : Failed to remove the existing associated address. The record failed to save after its foreign key was set to nil 当我第一次尝试填充街道时它工作,但当我尝试更新我得到错误: Failed to remove the existing associated address. The record failed to save after its foreign key was set to nil Failed to remove the existing associated address. The record failed to save after its foreign key was set to nil

any idea where is the error ? 任何想法错误在哪里? thank's 谢谢

There is an option to make it do a partial update if the record already exists: 如果记录已存在,可以选择进行部分更新:

accepts_nested_attributes_for(:address, update_only: true)

Documented here: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for 记录在这里: http//api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for

in your controller UsersController , in the update method, add the address: :id to the address permitted attributes. 在您的控制器UsersController ,在update方法中,将address: :id添加到允许的地址属性中。 Like this: 像这样:

params.require(:user).permit(:user_name, address_attributes: [:id, :street]))

This error usually indicates that there is an existing record for the has_one relationship. 此错误通常表示has_one关系存在现有记录。 In other words, this particular user object already has an address record associated with it. 换句话说,该特定user对象已经具有与之关联的address记录。 This could happen while testing the form in the browser. 在浏览器中测试表单时可能会发生这种情况。

In this case, it seems like Rails is trying to create a new address record, and it has to do with how your edit action is written. 在这种情况下,它似乎像Rails正在试图建立一个新的地址记录,以及它与你怎么做edit的动作写。

Try this: 尝试这个:

def edit
  @user = User.find(params[:id]) 
  @address = user.address || @user.build_address
end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM