1

私はRORを初めて使用し、Lynda.comチュートリアルを終了します。認証に関するセクションを完了しましたがwrong number of arguments、Railsコンソールでユーザープロファイルへの変更を保存しようとするとエラーが発生します。

これは私がコンソールから得た結果です:

>Loading development environment (Rails 3.2.9)
>> user = AdminUser.find(1)
>  AdminUser Load (0.4ms)  SELECT `admin_users`.* FROM `admin_users` WHERE                 `admin_users`.`id` = 1 LIMIT 1
=> #<AdminUser id: 1, first_name: "Stuart", last_name: "Culpepper", email:     "stuart.culpepper@gmail.com", hashed_password: "962b8cf45f025b7e1d155529f8c4bf1c02782164",     created_at: "2012-12-20 22:12:43", updated_at: "2012-12-28 20:04:42", username: "stuartculpepper", salt: "f3e2b128c35f8ec163caed28c4addc9d8de2d186">
>> user.password = 'secret'
=> "secret"
>> user.save
   (0.1ms)  BEGIN
  AdminUser Exists (0.4ms)  SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'stuartculpepper' AND `admin_users`.`id` != 1) LIMIT 1
   (0.1ms)  ROLLBACK
ArgumentError: wrong number of arguments (2 for 1)
    from /Users/stuartculpepper/Sites/simple_cms/app/models/admin_user.rb:60:in `hash_with_salt'
    from /Users/stuartculpepper/Sites/simple_cms/app/models/admin_user.rb:60:in `create_hashed_password'

これが私のUserAdminモデルのコードです。60行目に記載されているエラーは次のようになりますself.hashed_password = AdminUser.hash_with_salt(password, salt)

require 'digest/sha1'

class AdminUser < ActiveRecord::Base

  # set_table_name("admin_users")

  has_and_belongs_to_many :pages
  has_many :section_edits
  has_many :sections, :through => :section_edits

  attr_accessor :password

  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

  #validates_presence_of :first_name
  #validates_length_of :first_name, :maximum => 25
  #validates_presence_of :last_name
  #validates_length_of :last_name, :maximum => 50
  #validates_presence_of :username
  #validates_length_of :username, :within => 8..25
  #validates_uniqueness_of :username
  #validates_presence_of :email
  #validates_length_of :email, :maximum => 100
  #validates_format_of :email, :with => EMAIL_REGEX
  #validates_confirmation_of :email

  #sexy validations
  validates :first_name, :presence => true, :length => {:maximum => 25}
  validates :last_name, :presence => true, :length => {:maximum => 50}
  validates :username, :length => {:within => 8..25}, :uniqueness => true
  validates :email, :presence => true, :length => {:maximum => 100}, :format => EMAIL_REGEX, :confirmation => true 


  # only on create, so other aspects of this user can be updated
  validates_length_of :password, :within => 8..25, :on => :create

  before_save :create_hashed_password
  after_save :clear_password

  scope :named, lambda {|first,last| where(:first_name => first, :last_name => last)}

  attr_accessible :first_name, :last_name, :email, :username 
  attr_protected :hashed_password, :salt

  def self.make_salt(username="")
    Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
  end

  def self.hash_with_salt(password="")
    Digest::SHA1.hexdigest("Put #{salt} on the #{password}")
  end

   private

   def create_hashed_password
     # Whenever :password has a value hashing is needed
     unless password.blank?
       # alwys use "self" when assigning values
       self.salt = AdminUser.make_salt(username) if salt.blank?
       self.hashed_password = AdminUser.hash_with_salt(password, salt)
     end
   end

   def clear_password
     # for security and b/c hashing is not needed
     self.password = nil
   end

end

どんな考えでも歓迎します、ありがとう、そしてエラーが私の目の前にあるならば、私は謝罪します。私はstackoverflowに関する同様の問題をすべて調べましたが、どのように修正するのか理解できないようですが、これからも試していきます。;-)

4

1 に答える 1

1

2番目の引数を追加するのを忘れたようですsalt

def self.hash_with_salt(password="")
                                  ^

そのため、電話をかけるとエラーが発生します。

self.hashed_password = AdminUser.hash_with_salt(password, salt)
于 2012-12-29T17:03:37.083 に答える