Ruby on rails 轨道5。一对一关联,更新记录创建新记录
模型用户:Ruby on rails 轨道5。一对一关联,更新记录创建新记录,ruby-on-rails,associations,one-to-one,Ruby On Rails,Associations,One To One,模型用户: class User < ApplicationRecord has_one :address, foreign_key: :user_id accepts_nested_attributes_for :address end class用户“更新表单”}do | f |%> 当我提交表单时,它显示一切正常,我的意思是“更新成功”,但在数据库中,它的新记录似乎添加到了地址表中,但用户表已正确更新。有人能给我解释一下原因吗?我正在谷歌上寻找答案,但没有任何帮助 我的意
class User < ApplicationRecord
has_one :address, foreign_key: :user_id
accepts_nested_attributes_for :address
end
class用户
模型地址
class Address < ApplicationRecord
belongs_to :user, optional: true
end
类地址
控制器用户,一切都发生在这里
class UsersController < ApplicationController
def home # method which I use to display form
@user = User.find_by :id => session[:id]
end
def update # method for updating data
@user = User.find(session[:id])
if @user.update(user_params)
flash[:notice] = "Update successfully"
redirect_to home_path
else
flash[:error] = "Can not update"
redirect_to home_path
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, images_attributes: [:image_link, :image_description], address_attributes: [:city, :street, :home_number, :post_code, :country])
end
end
class UsersControllersession[:id]
结束
def update#更新数据的方法
@user=user.find(会话[:id])
如果@user.update(用户参数)
flash[:注意]=“更新成功”
重定向到主路径
其他的
闪存[:错误]=“无法更新”
重定向到主路径
结束
结束
私有的
def用户参数
参数要求(:用户)。允许(:姓名,:电子邮件,:密码,图像属性:[:图像链接,:图像描述],地址属性:[:城市,:街道,:家庭号码,:邮政编码,:国家])
结束
结束
更新表格:
<%= form_for @user, :html => { :id => "update-form", :class => "update-form"} do |f| %>
<%= f.text_field :name %>
<%= f.text_field :email %>
<%= f.fields_for :address do |a| %>
<%= a.text_field :city %>
<%= a.text_field :street %>
<%= a.number_field :home_number %>
<%= a.text_field :post_code %>
<%= a.text_field :country %>
<% end %>
<%= f.submit %>
<% end %>
{:id=>“更新表单”,:class=>“更新表单”}do | f |%>
当我提交表单时,它显示一切正常,我的意思是“更新成功”,但在数据库中,它的新记录似乎添加到了地址表中,但用户表已正确更新。有人能给我解释一下原因吗?我正在谷歌上寻找答案,但没有任何帮助
我的意思是,当我提交表格时,它会告诉我一切都很好
“更新成功”,但在数据库中,新记录看起来是
已添加到地址表,但用户表已正确更新。可以
有人给我解释为什么
这是由于强参数的性质造成的。它希望允许:id
正确更新嵌套的_属性,否则将创建新记录。允许:id
,您就可以出发了
def user_params
params.require(:user).permit(:name, :email, :password, images_attributes: [:id, :image_link, :image_description], address_attributes: [:id, :city, :street, :home_number, :post_code, :country])
end
在控制器中尝试以下代码:
class UsersController < ApplicationController
def home # method which I use to display form
@user = User.find_by :id => session[:id]
end
def update # method for updating data
@user = User.find(session[:id])
if @user.update(user_params)
flash[:notice] = "Update successfully"
redirect_to home_path
else
flash[:error] = "Can not update"
redirect_to home_path
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, images_attributes: [:image_link, :image_description], address_attributes: [:id, :city, :street, :home_number, :post_code, :country])
end
end
class UsersControllersession[:id]
结束
def update#更新数据的方法
@user=user.find(会话[:id])
如果@user.update(用户参数)
flash[:注意]=“更新成功”
重定向到主路径
其他的
闪存[:错误]=“无法更新”
重定向到主路径
结束
结束
私有的
def用户参数
参数require(:user).permit(:name,:email,:password,images\u attributes:[:image\u link,:image\u description],address\u attributes:[:id,:city,:street,:home\u number,:post\u code,:country])
结束
结束