Ruby on rails 为什么Rails 5没有保存嵌套属性,因为父模型没有先保存
我正在使用Rails 5和它的最新稳定版本。因此,我得到以下信息: 您已将关联设置为“必需”,但它已丢失。 在rails 5中,关联默认设置为required 要使其中一个为空,需要在中的关联上设置optional:true 模式 这是伟大的,我理解正在发生的事情,但是在我的一生中,我不知道如何首先保存父模型,因此用户id被转换为嵌套模型记录。我在上面到处都看到了相同的答案,但是除了将初始值设定项中的默认值从true改为false之外,没有人解释其他工作。这并不能解决问题,因为记录确实保存了,但不包含用户id 下面是我的代码库,我想问,而不是用上面的引语回答,有人能告诉我如何在保存时将USER_ID字段放入嵌套属性中吗。我拒绝禁用验证并手动处理插入,因为这不是ruby方式,并且与标准背道而驰! 提前感谢所有能够直接回答这个问题的人,他们没有离题的模糊解释Ruby on rails 为什么Rails 5没有保存嵌套属性,因为父模型没有先保存,ruby-on-rails,activerecord,ruby-on-rails-5,nested-forms,nested-attributes,Ruby On Rails,Activerecord,Ruby On Rails 5,Nested Forms,Nested Attributes,我正在使用Rails 5和它的最新稳定版本。因此,我得到以下信息: 您已将关联设置为“必需”,但它已丢失。 在rails 5中,关联默认设置为required 要使其中一个为空,需要在中的关联上设置optional:true 模式 这是伟大的,我理解正在发生的事情,但是在我的一生中,我不知道如何首先保存父模型,因此用户id被转换为嵌套模型记录。我在上面到处都看到了相同的答案,但是除了将初始值设定项中的默认值从true改为false之外,没有人解释其他工作。这并不能解决问题,因为记录确实保存了,但
###Models
#Users
class User < ApplicationRecord
has_one :profile, inverse_of: :user
accepts_nested_attributes_for :profile, allow_destroy: true
end
#Profiles
class Profile < ApplicationRecord
belongs_to :user, inverse_of: :profile
end
###Controller
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
@user.build_profile
end
# GET /users/1/edit
def edit
@user.build_profile
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:username, :password, :user_type_id, profile_attributes: [:user_id, :first_name, :middle_name, :last_name, :phone_number, :cell_number, :email])
end
end
##View
<%= form_for(@user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
<!--<li><%= debug f %></li>-->
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<% if params[:trainer] == "true" %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '2' %>
<% else %>
<%= f.label :user_type_id %>
<%= f.text_field :user_type_id, :readonly => true, :value => '1' %>
<% end %>
</div>
<h2>Account Profile</h2>
<%= f.fields_for :profile do |profile| %>
<%#= profile.inspect %>
<div>
<%= profile.label :first_name %>
<%= profile.text_field :first_name %>
</div>
<div>
<%= profile.label :middle_name %>
<%= profile.text_field :middle_name %>
</div>
<div>
<%= profile.label :last_name %>
<%= profile.text_field :last_name %>
</div>
<div>
<%= profile.label :email %>
<%= profile.text_field :email %>
</div>
<div>
<%= profile.label :phone_number %>
<%= profile.telephone_field :phone_number %>
</div>
<div>
<%= profile.label :cell_phone %>
<%= profile.telephone_field :cell_number %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<%= debug params %>
<%= debug user %>
<%= debug user.profile %>
<% end %>
好的,我在回答我自己的问题,因为我知道很多人都在努力解决这个问题,我实际上有答案,而不是对文档的模糊回答 首先,我们将在本例中使用一对一关系。创建关系时,需要确保父模型具有以下特性
class User < ApplicationRecord
has_one :profile, inverse_of: :user, autosave: true
accepts_nested_attributes_for :profile, allow_destroy: true
end
如果您查看它,它将从头开始重建配置文件,并将与您正在编辑的当前用户匹配的记录的用户id重置为null
所以要非常小心,因为我看到很多帖子都在提出这个建议,我花了好几天的时间研究来找到解决方案 好的,我在回答我自己的问题,因为我知道很多人都在努力解决这个问题,我实际上已经得到了答案,而不是对文档的模糊回答 首先,我们将在本例中使用一对一关系。创建关系时,需要确保父模型具有以下特性
class User < ApplicationRecord
has_one :profile, inverse_of: :user, autosave: true
accepts_nested_attributes_for :profile, allow_destroy: true
end
如果您查看它,它将从头开始重建配置文件,并将与您正在编辑的当前用户匹配的记录的用户id重置为null
所以要非常小心,因为我看到很多帖子都在提出这个建议,我花了好几天的时间研究来找到解决方案 Rails官方文档中详细介绍了
autosave
选项。Rails官方文档中详细介绍了autosave
选项。您不需要在编辑操作中使用@user.build\u profile
的原因是您已经在保存与您的用户关联的profile
记录。如果没有配置文件
记录的用户转到“编辑用户”页面,他们将永远看不到配置文件的表单。所以在调用@user.build\u profile
之前,只需检查配置文件是否存在。正如我在回复中所说,我意识到了这一点,我在这里解释这一点,因为网络上的人们似乎喜欢在这方面给出错误的建议,作为一名经验丰富的专业人士,我有一个错误信息的大问题。但你没有解释为什么你根本不需要这些。你刚才说那样做是不对的。就像我说的,这是个案基础上的。如果它在你的应用程序上不起作用,并不意味着它在其他应用程序上不起作用。从文档中可以看出,除非你将它封装在检查记录是否存在的检查中,否则它永远不应该在编辑中。但是,如果你的应用程序设置正确,那就不需要了。如果你仔细阅读我的文章,你会注意到我提到了这个问题,但这不是我文章的重点。您的评论让人觉得您是在吹毛求疵,而不是出于其预期目的阅读文章,即在不删除验证的情况下,让嵌套模型与父对象一起正确保存。听起来你只是想批评某人。我也遇到了同样的问题,但只添加了属性的逆属性就解决了我的问题(没有声明autosave)。在编辑操作中不需要@user.build\u profile
的原因是你已经保存了与你的用户关联的profile
记录。如果没有配置文件
记录的用户转到“编辑用户”页面,他们将永远看不到配置文件的表单。所以在调用@user.build\u profile
之前,只需检查配置文件是否存在。正如我在回复中所说,我意识到了这一点,我在这里解释这一点,因为网络上的人们似乎喜欢在这方面给出错误的建议,作为一名经验丰富的专业人士,我有一个错误信息的大问题。但你没有解释为什么你根本不需要这些。你刚才说那样做是不对的。就像我说的,这是个案基础上的。如果它在你的应用程序上不起作用,并不意味着它在其他应用程序上不起作用。从文档中可以看出,除非你将它封装在检查记录是否存在的检查中,否则它永远不应该在编辑中。但是,如果你的应用程序设置正确,那就不需要了。如果你仔细阅读我的文章,你会注意到我提到了这个问题,但这不是我文章的重点。您的评论让人觉得您是在吹毛求疵,而不是出于其预期目的阅读文章,即在不删除验证的情况下,让嵌套模型与父对象一起正确保存。听起来你只是想批评某人。我也有同样的问题,但是
class User < ApplicationRecord
has_one :profile, inverse_of: :user, autosave: true
accepts_nested_attributes_for :profile, allow_destroy: true
end
Started GET "/users/1/edit" for 192.168.0.31 at 2017-03-12 22:38:17 -0400
Cannot render console from 192.168.0.31! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Profile Load (0.5ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
(0.1ms) BEGIN
SQL (0.5ms) UPDATE `profiles` SET `user_id` = NULL, `updated_at` = '2017-03-13 02:38:17' WHERE `profiles`.`id` = 1
(59.5ms) COMMIT
Rendering users/edit.html.erb within layouts/application
Rendered users/_form.html.erb (44.8ms)
Rendered users/edit.html.erb within layouts/application (50.2ms)
Completed 200 OK in 174ms (Views: 98.6ms | ActiveRecord: 61.1ms)