【发布时间】:2014-04-30 01:18:55
【问题描述】:
我在 Ruby On rails 中创建了一个项目,并且我有这样的控制器用户:
class UsersController < ApplicationController
before_action :set_user, only: [:edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
像这样模拟用户:
class User < ActiveRecord::Base
#attr_accessible :email, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
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
当我尝试创建新用户时出现错误:ActiveModel::ForbiddenAttributesError
参数:
{"utf8"=>"✓",
"authenticity_token"=>"74Mhbpn9FF/tY/cgfuVmX7ribN4rOkkdUjSgbLNsces=",
"user"=>{"email"=>"henio180@interia.pl",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"button"=>""}
我正在使用 Ruby on Rails 4.0.3。
【问题讨论】:
-
强参数吸引了许多旧版 Rails 开发人员。这是另一个类似的问题:stackoverflow.com/questions/18679758/…
标签: ruby-on-rails ruby ruby-on-rails-4 strong-parameters