我正在尝试使用has_secure_password的简单身份验证系统,我收到了标题中列出的错误。我已经多次看过这个问题,但其他人的建议或修正都不适合我。我正处于这样的地步,我确信我必须有一个简单的错误,但我找不到。发布在下面是我的相关代码:
的Gemfile:
gem 'rails', '3.2.13'
gem 'bcrypt-ruby'
users_controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params{:user})
if @user.save
session[:user_id] = @user.id
redirect_to root_url, notice: "User created"
else
render "new"
end
end
end
用户模型:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :email, :password, :password_confirmation
validates :password, presence: true, length: { minimum: 6 }
validates_confirmation_of :password
validates :password_confirmation, presence: true
end
用户表单:
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit "Sign Up" %></div>
<% end %>
迁移:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password_digest
t.timestamps
end
end
end
以下是我得到的实际错误:
Can't mass-assign protected attributes: utf8, authenticity_token, user, commit, action, controller
提前感谢您对我的困境的任何见解。
答案 0 :(得分:2)
您正在分配完整的参数哈希,其中包含所有提到的字段。这是原因
@user = User.new(params{:user})
应该是
@user = User.new(params[:user])