尝试在 Rails 中保存模型对象时出现错误。让我说一下,我没有使用数据库迁移,而是使用带有 Rails 的预先存在的数据库。 这是我的模型类:
require 'bcrypt'
require 'securerandom'
class Profile < ActiveRecord::Base
include BCrypt
self.table_name = 'profiles'
self.primary_key = 'id'
attr_accessor :id, :username, :password_hash, :salt, :first_name, :last_name, :location, :status, :game_status
def initialize(attributes = {}, options = {})
@username = attributes[:username]
@salt = SecureRandom.hex
@password_hash = Password.create(attributes[:password] + @salt).to_s
@first_name = attributes[first_name]
@last_name = attributes[last_name]
@location = attributes[location]
@status = "Hi"
@game_status = "Playing some game..."
end
def hash_rep
hash = {}
hash['id'] = @id
hash['username'] = @username
hash['password_hash'] = @password_hash
hash['salt'] = @salt
hash['location'] = @location
hash['status'] = @status
hash['game_status'] = @game_status
return hash
end
end
这是我的数据库架构:
id int Unsigned NOT NULL AUTO_INCREMENT
username varchar(16) NOT NULL
password_hash tinytext NOT NULL
salt varchar(64) NOT NULL
first_name varchar(16) NOT NULL
last_name varchar(16) NOT NULL
location tinytext NOT NULL
status tinytext NULL
game_status tinytext NULL
这是我的 Controller 代码:
def register
profile = Profile.new(:id => params[:id],
:username => params[:username],
:password => params[:password],
:first_name => params[:first_name],
:last_name => params[:last_name],
:location => params[:location])
profile.save
render_profile(profile)
end
“profile.save”方法发生错误。这是相关的堆栈跟踪:
activerecord (4.2.0) lib/active_record/transactions.rb:375:in `clear_transaction_record_state'
activerecord (4.2.0) lib/active_record/transactions.rb:306:in `ensure in rollback_active_record_state!'
activerecord (4.2.0) lib/active_record/transactions.rb:306:in `rollback_active_record_state!'
activerecord (4.2.0) lib/active_record/transactions.rb:285:in `save'
app/controllers/profile_controller.rb:52:in `register'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
错误提示:“nil:NilClass 的未定义方法‘[]’”
最佳答案
require 'bcrypt'
require 'securerandom'
class Profile < ActiveRecord::Base
include BCrypt
self.table_name = 'profiles'
self.primary_key = 'id'
def hash_rep
hash = {}
hash['id'] = id
hash['username'] = username
hash['password_hash'] = password_hash
hash['salt'] = salt
hash['location'] = location
hash['status'] = status
hash['game_status'] = game_status
hash
end
def self.build(args)
new_profile = Profile.new
new_profile.username = args[:username]
salt = SecureRandom.hex
new_profile.password_hash = Password.create(args[:password] + salt).to_s
new_profile.first_name = args[:first_name]
new_profile.last_name = args[:last_name]
new_profile.location = args[:location]
new_profile.status = "Hi"
new_profile.game_status = "Playing some game..."
new_profile
end
end
现在你可以像这样使用它:
Profile.build({ username: 'foo' })
顺便说一句,你的 hash_rep 方法没那么有用,尝试一下:
profile = Profile.build({ username: 'foo' })
profile.attributes
旁注:
由于您遵循约定,因此不需要添加这些行,只需删除它们即可:
self.table_name = 'profiles'
,self.primary_key = 'id'
小心哈希值,似乎您不关心字符串或符号键,但它们并不相同
有更优雅的方式来编写你的方法,但我保持简单,因为在这个阶段没有必要详细说明
关于ruby-on-rails - Rails ActiveRecord 保存错误未定义方法 `[]' for nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29791286/