简体   繁体   English

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

[英]ArgumentError in UsersController#create; Wrong number of arguments (2 for 0)

I'm getting the error "wrong number of arguments (2 for 0) from 'build_profile' function in user.rb while trying to add a profile to a user using the one to one relationship. 尝试使用一对一关系向用户添加配置文件时,我从user.rb中的'build_profile'函数收到错误“参数数量错误(2为0)”。

Models 楷模

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

end 结束

I get this error when a user tries to sign up , below is the sign up form 当用户尝试注册时出现此错误,下面是注​​册表单

Sign Up Form 报名表格

<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 %>

Thanks 谢谢

Server Log Output for posting the user new form 服务器日志输出,用于发布用户新表单

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)

It's because build_profile is a method already defined by Rails. 这是因为build_profile是Rails已经定义的方法。

Try changing the name: 尝试更改名称:

after_create :build_default_profile

def build_default_profile
  Profile.create(user: self)
end

Since you are already defining a relationship for these two models (user has_one :profile), the build_profile is already defined for you. 由于您已经为这两个模型(用户has_one:profile)定义了关系,因此已经为您定义了build_profile。 No need to do so in your model. 您的模型中无需这样做。 take a look at http://edgeguides.rubyonrails.org/association_basics.html for more details. 有关更多详细信息,请访问http://edgeguides.rubyonrails.org/association_basics.html

I have been able to finally found a solution to both queries i had; 我终于能够找到我所遇到的两个查询的解决方案; the latter resulting from attempted solution from my first question. 后者来自我第一个问题的尝试解决。

A. ArgumentError in UsersController#create; A. UsersController#create中的ArgumentError; Wrong number of arguments (2 for 0) 参数数量错误(2代表0)

B. Form_for taking in values and not posting to database. B. Form_for接受值并且不发布到数据库。

I am posting my solution here for future references for any one run into either problems with their codes. 我在这里发布我的解决方案,以供将来遇到任何有关其代码问题的参考。

Andrew, Thanks for referring me to the guide, it was an educational read. 安德鲁,感谢您向我介绍该指南,这是一本教育性的著作。 Juanpastas, Thanks. Juanpastas,谢谢。 You helped solved both queries, though i ended up using Profile.create(user_id: self.id) instead of Profile.create(user: self) due to trouble with mass assignment of protected values. 尽管我最终使用Profile.create(user_id: self.id)而不是Profile.create(user: self)来解决两个查询,但由于批量分配受保护的值而遇到麻烦。 Your comment requesting the server log output made me scrutinize the log and noticed that it was rolling back the parameters instead of saving them to the database. 您要求服务器日志输出的评论使我仔细检查了日志,并注意到它正在回滚参数,而不是将其保存到数据库中。 Through research and other post on here such as, how to find the cause of ActiveRecord ROLLBACK (Serge Seletskyy's answer), and Max Wong's answer in Why does my rails rollback when I try to user.save? 通过研究和此处的其他文章,例如, 如何找到ActiveRecord ROLLBACK的原因 (Serge Seletskyy的答案)以及Max Wong的答案, 当我尝试user.save时为什么我的rails回滚? , I learned it might be due to failing validations Using User.save! ,我了解到这可能是由于使用User.save!验证User.save! as the if condition in my User's create function helped see that some other attributes and the line validates :address, :age, :sex, presence: true in the profile model was making the callback function to fail validation as they can't be blank and return false, causing the rollback by the server, but commenting out the line, #validates :address, :age, :sex, presence: true , solved it. 因为我用户的create函数中的if条件有助于查看其他一些属性,并且该行validates :address, :age, :sex, presence: true在配置文件模型中为validates :address, :age, :sex, presence: true ,这使回调函数无法通过验证,因为它们不能为空并返回false,导致服务器回滚,但注释掉以下行: #validates :address, :age, :sex, presence: true ,解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM