我正在运行我的rails服务器,试图注册用户。我收到'(field name)' can't be blank
错误。当我通过Rails控制台创建用户时,我没有错误,但是当我尝试通过服务器时,它所说的字段为空白(不是)::first_name
,:last_name
,:email
,:password
和:password_confirmation
(我通过服务器输入所有这些字段)。
我认为这可能是与Devise有关的错误,但我不再那么肯定,因为我将新用户视图切换到Devise的注册视图以及免费路由。
用户控制器:
class UsersController < ApplicationController
before_filter :signup_params
def new
@user = User.new
render 'new'
end
def create
@user = User.new(signup_params)
if @user.save
sign_in @user
redirect_to @user
else
render 'new'
end
end
def signup_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
end
用户模型:
class User < ActiveRecord::Base
validates_presence_of :first_name, :last_name, :email, :password, :password_confirmation #tried removing this due to strong params, to no avail
validates_uniqueness_of :email
devise :database_authenticatable, :timeoutable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
before_save :create_session_token
def create_session_token
self.session_token = SecureRandom.urlsafe_base64
end
end
架构(针对用户):
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "session_token"
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name"
t.string "last_name"
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
视图/设计/注册/新:
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.text_field :first_name ,placeholder: 'First Name', class: 'signUpForm', id: 'firstName' %>
<%= f.text_field :last_name ,placeholder: 'Last Name', class: 'signUpForm', id: 'lastName' %>
<%= f.email_field :email ,placeholder: 'Email', class: 'signUpForm', id: 'email' %>
<%= f.password_field :password ,placeholder: 'Password', class: 'signUpForm', id: 'password' %>
<%= f.password_field :password_confirmation ,placeholder: 'Confirm Password', class: 'signUpForm', id: 'confirm_password' %>
<%= f.submit class: 'signUpForm btn btn-inverse', id: 'submit'%>
<% end %>
<%= render "devise/shared/links" %>
如果有人可以分享关于为什么这些字段没有通过服务器保存的任何建议,我将非常感激。提前谢谢。
堆栈跟踪:
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4KvHBYLTXs+wRAyB/1d5DKRVRqWO3eFKWyFwlbQ+dSE=", "user"=> {"first_name"=>"adfs", "last_name"=>"asdffasd", "email"=>"asdfsd@email.arizona.edu", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
Unpermitted parameters: first_name, last_name
WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation
(0.1ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY '' LIMIT 1
(0.1ms) ROLLBACK
Rendered devise/shared/_links.erb (1.8ms)
Rendered devise/registrations/new.html.erb within layouts/application (27.8ms)
Completed 200 OK in 502ms (Views: 143.1ms | ActiveRecord: 2.1ms)
更新错误:
Started POST "/users" for 127.0.0.1 at 2014-02-26 15:50:44 -0700
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4KvHBYLTXs+wRAyB/1d5DKRVRqWO3eFKWyFwlbQ+dSE=", "user"=>{"first_name"=>"adfs", "last_name"=>"asdffasd", "email"=>"asdfsd@email.arizona.edu", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
WARNING: Can't mass-assign protected attributes for User: email, password, password_confirmation, first_name, last_name
答案 0 :(得分:3)
根据错误:“未经许可的参数:first_name,last_name”
您需要允许他们使用Devise Controller。
在ApplicationController
添加此内容:
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name << :last_name
end
编辑
此外,删除protected_attributes
gem,因为它与强参数冲突。