当我试图使用一对一关系向用户添加配置文件时,从user.rb中的'build_profile'函数中得到错误的"参数数量错误(0为2)"。
型号
用户.rb
class User < ActiveRecored::Base
has_one :profile, dependent: :destroy
after_create :build_profile
accepts_nested_attributes_for :profile
attr_accessible :email, :password, :password_confirmation, :profile_attributes
def build_profile
Profile.create(user: self)
end
end
简介.rb
class Profile < ActiveRecord::Base
attr_accessible :name. :address, :age
belongs_to :user
validates :name, presence: true, length: { maximum: 50 }
validates :address, :age, presence: true
end
用户_控制器.rb
class UsersController < ApplicationController
def new
@user = User.new
@profile = self.build_profile
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "welcome, Thanks for Signing up"
redirect_to @user
else
render 'new'
end
end
end
Profiles_controller.rb
class ProfilesController < ApplicationController
def edit
@profile = Profile.find(params[:id])
end
def show
@user.profile = User.find(params[:id]).profile
end
def destroy
end
结束
当用户试图注册时,我会收到这个错误,下面是注册表单
注册表格
<h1> Sign Up </h1>
<div class="span6 offset3">
<%= form_for (setup_user(@user)) do |f| %>
<%= f.fields_for :profile do |ff| %>
<p>
<%= ff.label :name %>
<%= ff.text_field :name %>
</p>
<% end %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.pasword_field :password %>
</p>
<p>
<%= f.label :password_conformation, "Confirm Password" %>
<%= f.password_field :password_confirmation%>
</p>
<%= f.submit "create my account", class: "btn btn-large btn-primary" %>
<% end %>
感谢
用于发布用户新表单的服务器日志输出
Started POST "/users" for 127.0.0.1 at 2014-10-13 10:01:42 -0400
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"G£ô", "authenticity_token"=>"7PQwoH4VGDE2kyHqjkv4PegFz/A
KYGYXRFtQpn9UKko=", "user"=>{"profile_attributes"=>{"name"=>"Example Test"}, "em
ail"=>"example@test.com", "password"=>"[FILTERED]", "password_confirmation"=>"[F
ILTERED]"}, "commit"=>"Create my account"}
(0.0ms) BEGIN
User Exists (25.0ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = 'ex
ample@test.com' LIMIT 1
(0.0ms) ROLLBACK
Rendered users/new.html.erb within layouts/application (4.0ms)
User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`remember_token
` IS NULL LIMIT 1
Rendered layouts/_header.html.erb (3.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 330ms (Views: 93.0ms | ActiveRecord: 26.0ms)
build_profile
是Rails已经定义的方法。
尝试更改名称:
after_create :build_default_profile
def build_default_profile
Profile.create(user: self)
end
由于您已经为这两个模型定义了关系(userhas_one:profile),因此已经为您定义了build_profile。在您的模型中不需要这样做。看看http://edgeguides.rubyonrails.org/association_basics.html了解更多详细信息。
我终于找到了两个查询的解决方案;后者是我试图解决第一个问题的结果。
A。UsersController#create;中的ArgumentError;参数数量错误(0为2)
B。Form_for接受值而不过帐到数据库。
我在这里发布了我的解决方案,以供未来任何一个遇到代码问题的人参考。
安德鲁,谢谢你介绍我看指南,这是一本教育读物。Juanpastas,谢谢。您帮助解决了这两个查询,但由于大量分配受保护值的问题,我最终使用了Profile.create(user_id: self.id)
而不是Profile.create(user: self)
。你请求服务器日志输出的评论让我仔细查看了日志,并注意到它正在回滚参数,而不是将它们保存到数据库中。通过研究和其他帖子,例如,如何找到ActiveRecord ROLLBACK的原因(Serge Seletskyy的答案),以及Max Wong在《当我尝试user.save时,为什么我的rails会回滚?》中的答案,我了解到这可能是由于验证失败在我的User的创建函数中使用User.save!
作为if条件有助于发现其他一些属性和配置文件模型中的行validates :address, :age, :sex, presence: true
使回调函数无法通过验证,因为它们不能为空并返回false,导致服务器回滚,但注释掉行#validates :address, :age, :sex, presence: true
解决了这个问题。