簡體   English   中英

Rails db:種子“rake aborted! ArgumentError:錯誤的參數數量(2為0..1)“

[英]Rails db:seed “rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)”

我正在做一個噩夢,試圖將用戶數據播種到我的rails項目中。 我正在使用Nitrous.io,我可以通過在控制台中鍵入以下內容來設置新用戶

rails console 

然后在控制台中執行以下操作

2.1.1 :001 > u = User.new                                                                                                                                                             
=> #<User id: nil, username: nil, email: nil, password_hash: nil, password_salt: nil, admin: nil, points: nil, leagues: nil>                                                         
2.1.1 :002 > u.username = 'testadmin'  
=> "testadmin"                                                                                                                                                                       
2.1.1 :003 > u.email = 'testAdmin@test.com'                                                                                                                                           
=> "testAdmin@test.com"                                                                                                                                                              
2.1.1 :004 > u.password = 'test'                                                                                                                                                      
=> "test" 
2.1.1 :005 > u.password_confirmation = 'test'                                                                                                                                         
=> "test"                                                                                                                                                                            
2.1.1 :006 > u.admin = true                                                                                                                                                           
=> true                                                                                                                                                                              
2.1.1 :007 > u.save  
   (86.8ms)  BEGIN                                                                                                                                                                    
User Exists (87.5ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = 'testAdmin@test.com' LIMIT 1                                                                           
User Exists (86.6ms)  SELECT  1 AS one FROM "users"  WHERE "users"."username" = 'testadmin' LIMIT 1                                                                                 
SQL (91.7ms)  INSERT INTO "users" ("admin", "email", "password_hash", "password_salt", "username") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["admin", "t"], ["email", "testAdmin @test.com"], ["password_hash", "$2a$10$tVMzM58wjt.lDG6dlsoLQe4obwYzLf3UfUc2ui6MD2eTGAgwTTaQK"], ["password_salt", "$2a$10$tVMzM58wjt.lDG6dlsoLQe"], ["username", "testadmin"]]        
(89.3ms)  COMMIT                                                                                                                                                                   
=> true                                                                                                                                                                              
2.1.1 :008 > Exit

然后我轉到項目中的seeds.rb文件並輸入以下內容

 User.create ({username: 'testadmin2', email: 'testAdmin2@test.com', password: 'test', password_confirmation: 'test', admin: true})

然后我在控制台中運行rake db:seed但得到以下錯誤:

rake aborted!                                                                                                                                                                         
ArgumentError: wrong number of arguments (2 for 0..1)                                                                                                                                 
/home/action/workspace/PAW/app/models/user.rb:39:in `initialize'                                                                                                                      
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'                                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/inheritance.rb:30:in `new'                                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/protected_attributes-1.0.7/lib/active_record/mass_assignment_security/persistence.rb:45:in `create'                                            
/home/action/workspace/PAW/db/seeds.rb:8:in `<top (required)>'  
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                           
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'                                                                  
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'                                                                           
/home/action/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/engine.rb:543:in `load_seed'      
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'                                                                
/home/action/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'                                     
Tasks: TOP => db:seed                                                                                                                                                                 
(See full trace by running task with --trace)   

我是ruby和rails的新手,所以我可能做了一些愚蠢的事情,但是我在過去的兩個小時里一直在做這件事,我根本無法理解。 我想要像這樣播種數據的原因是因為我想設置一些可以訪問某些頁面的管理員(一切正常)。 為此,我需要設置admin = true。 如果我通過網站設置新用戶,我無法選擇將用戶設置為管理員。 我可以通過控制台按照上面的方式完成,但需要很長時間。 播種會更快,我也想知道我做錯了什么。 我將在下面給你一些其他代碼。 謝謝你的期待(並希望有所幫助)

user.rb

  class User < ActiveRecord::Base
    attr_accessible :email, :username, :password, :password_confirmation
    attr_accessor :password
    before_save :encrypt_password

    validates_confirmation_of :password
    validates_presence_of :password, :on => :create
    validates_presence_of :email, :on => :create
    validates_presence_of :username, :on => :create
    validates_uniqueness_of :email
    validates_uniqueness_of :username

    def self.authenticate_by_email(email, password)
      user = find_by_email(email)
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end

    def self.authenticate_by_username(username, password)
      user = find_by_username(username)
      if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
        user
      else
        nil
      end
    end

    def encrypt_password
      if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
      end
    end

    def initialize(attributes = {})
      super # must allow the active record to initialize!
      attributes.each do |name, value|
        send("#{name}=", value)
      end
    end    
  end

authentication_controller.rb

  class AuthenticationController < ApplicationController
    def sign_in
      @user = User.new
    end

    def login
      username_or_email = params[:user][:username]
      password = params[:user][:password]

      if username_or_email.rindex('@')
        email=username_or_email
        user = User.authenticate_by_email(email, password)
      else
        username=username_or_email
        user = User.authenticate_by_username(username, password)
      end

      if user
        session[:user_id] = user.id
        flash[:notice] = 'Welcome' 
        redirect_to :root
      else
        flash.now[:error] = 'Unknown user. Please check your username and password.'

        # Assign user to instance variable for the `sign_in` view!
        @user = User.new(params[:user]) 

        render :action => "sign_in"
      end
    end

    def signed_out
      session[:user_id] = nil
      flash[:notice] = "You have been signed out."
    end

    def new_user
      @user = User.new
    end

    def register
      @user = User.new(params[:user])

      if @user.valid?
        @user.save
        session[:user_id] = @user.id
        flash[:notice] = 'Welcome.'
        redirect_to :root
      else
        render :action => "new_user"
      end
    end

  end

問題現在回答了。 見下文。

  1. 不要在模型中這樣做( User.rb

     def initialize(attributes = {}) super # must allow the active record to initialize! attributes.each do |name, value| send("#{name}=", value) end end 

    這不是必需的。 Rails自動創建getter和setter方法。

  2. 而且,在使用create創建記錄時,不需要使用{}包裝參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM