Rails 4 nested_attributes 与 has_one 关联回滚
Rails 4 nested_attributes with has_one association rollback
我在 Rails 4 个应用程序中有两个模型与 has_one 关联。
class User < ActiveRecord::Base
has_one :member
accepts_nested_attributes_for :member
end
class Member < ActiveRecord::Base
belongs_to :user
end
我正在尝试创建带成员的用户。
这是我的控制器。
class UsersController < ApplicationController
def new
@user = User.new
@user.build_member
end
def create
@user = User.new member_params
if @user.save
redirect_to users_path
else
render action: :new
end
end
private
def member_params
params[:user].permit(:id, :name, :email, :password, :city, member_attributes: [ :position, :avatar, :avatar_cache, :user_id ])
end
end
这是表格。我使用 SimpleForm.
= simple_form_for @user do |f|
= f.input :name
= f.input :email
= f.input :city
= f.input :password
= f.simple_fields_for :member, @user.member do |us|
= us.input :position
= us.input :avatar
= f.button :submit
我的代码哪一部分有问题?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eqxcHlLrnKB9atm3YGG6WyMT429qqRr5zpB/LM4IMAI=", "user"=>{"name"=>"Павел", "email"=>"kalashnikov@ulmic.ru", "city"=>"Ульяновск", "password"=>"[FILTERED]", "member_attributes"=>{"position"=>"123", "avatar_cache"=>""}}, "commit"=>"Создать User"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 (0.3ms)
BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'kalashnikov@ulmic.ru' LIMIT 1 (0.3ms)
ROLLBACK
此代码在我的 Rails 3 项目中有效,但在 Rails 3 中没有 strong_parameters。
验证问题比强参数问题更有可能。在您的情况下, user.email
似乎被限制为唯一的,但它可以是 user
或 member
模型上的任何内容。当然,即使提交了正确的数据,禁止参数也可能是验证失败的原因,但在这种情况下,您会在服务器日志中看到一条消息(但不会抛出异常)。
我在 Rails 4 个应用程序中有两个模型与 has_one 关联。
class User < ActiveRecord::Base
has_one :member
accepts_nested_attributes_for :member
end
class Member < ActiveRecord::Base
belongs_to :user
end
我正在尝试创建带成员的用户。 这是我的控制器。
class UsersController < ApplicationController
def new
@user = User.new
@user.build_member
end
def create
@user = User.new member_params
if @user.save
redirect_to users_path
else
render action: :new
end
end
private
def member_params
params[:user].permit(:id, :name, :email, :password, :city, member_attributes: [ :position, :avatar, :avatar_cache, :user_id ])
end
end
这是表格。我使用 SimpleForm.
= simple_form_for @user do |f|
= f.input :name
= f.input :email
= f.input :city
= f.input :password
= f.simple_fields_for :member, @user.member do |us|
= us.input :position
= us.input :avatar
= f.button :submit
我的代码哪一部分有问题?
Parameters: {"utf8"=>"✓", "authenticity_token"=>"eqxcHlLrnKB9atm3YGG6WyMT429qqRr5zpB/LM4IMAI=", "user"=>{"name"=>"Павел", "email"=>"kalashnikov@ulmic.ru", "city"=>"Ульяновск", "password"=>"[FILTERED]", "member_attributes"=>{"position"=>"123", "avatar_cache"=>""}}, "commit"=>"Создать User"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 (0.3ms)
BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'kalashnikov@ulmic.ru' LIMIT 1 (0.3ms)
ROLLBACK
此代码在我的 Rails 3 项目中有效,但在 Rails 3 中没有 strong_parameters。
验证问题比强参数问题更有可能。在您的情况下, user.email
似乎被限制为唯一的,但它可以是 user
或 member
模型上的任何内容。当然,即使提交了正确的数据,禁止参数也可能是验证失败的原因,但在这种情况下,您会在服务器日志中看到一条消息(但不会抛出异常)。