对于红宝石来说还是新手,所以我不太清楚是什么 出错了。我得到一个错误说 “堆栈水平太深”我试图注册一个 新用户
这是我的代码:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params)
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
private
def params
params.require(:user).permit(:firstName, :secondName, :activated, :email, :password)
end
end
Users.rb
class User < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_presence_of :activated
validates_presence_of :firstName
validates_presence_of :secondName
validates_uniqueness_of :email
def self.authenticate(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 encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
完全错误是:
UsersController中的SystemStackError #create 堆栈级别太深
答案 0 :(得分:1)
您的params
方法需要被命名为params
之外的其他名称,例如safe_params
。否则,当你在下一行params.require
时,你就会自我调用。
params.require
正在访问来自Rails内的名为params
的方法。您params
'的def
方法正在覆盖Rails params
方法。
清晰度编辑:这应该是它的样子:
def create
@user = User.new(safe_params)
...
end
def safe_params
params.require(:user).permit(:firstName, :secondName, :activated, :email, :password)
end