UsersController中的ArgumentError #create;参数数量错误(0表示0)

时间:2014-10-07 19:32:12

标签: ruby-on-rails ruby-on-rails-3.2

我在尝试使用一对一关系向用户添加配置文件时,在user.rb中从'build_profile'函数收到错误的“错误的参数数量(2为0)”。

模型

User.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

Profile.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

Users_controller.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)

3 个答案:

答案 0 :(得分:1)

因为build_profile是Rails已经定义的方法。

尝试更改名称:

after_create :build_default_profile

def build_default_profile
  Profile.create(user: self)
end

答案 1 :(得分:0)

由于您已经为这两个模型(用户has_one:profile)定义了关系,因此已经为您定义了build_profile。您无需在模型中执行此操作。请查看http://edgeguides.rubyonrails.org/association_basics.html了解更多详情。

答案 2 :(得分:0)

我终于找到了解决这两个问题的方法;后者是我第一个问题尝试解决的结果。

一个。 UsersController中的ArgumentError #create;参数数量错误(2表示0)

B中。 Form_for接收值而不发布到数据库。

我在这里发布我的解决方案,以便将来参考任何一个遇到问题的代码。

安德鲁,感谢您引导我参考指南,这是一本教育读物。 Juanpastas,谢谢。您帮助解决了这两个问题,但由于质量分配受保护值的问题,我最终使用Profile.create(user_id: self.id)代替Profile.create(user: self)。 您的注释请求服务器日志输出让我仔细检查日志,并注意到它正在回滚参数而不是将它们保存到数据库。 通过这里的研究和其他帖子,如how to find the cause of ActiveRecord ROLLBACK(Serge Seletskyy的回答)和Max Wong在Why does my rails rollback when I try to user.save?的答案,我了解到这可能是由于验证失败 在我的用户创建函数中使用User.save!作为if条件有助于查看配置文件模型中的某些其他属性和行validates :address, :age, :sex, presence: true使回调函数无法通过验证,因为它们可以&#39 ; t为空并返回false,导致服务器回滚,但注释掉行#validates :address, :age, :sex, presence: true,解决了它。