[英]Rails - Devise error: “Email can't be blank, password can't be blank” when create new user
[英]Devise error: email can't be blank
(編輯:添加了 development.log 日志,“不允許的參數:電子郵件”)
Devise 工作正常,但我使用 rails 控制台刪除了所有用戶並嘗試創建一個新用戶。 我收到一個錯誤,即email can't be blank
。 當我刪除:validatable
塊時,我收到此錯誤。
我試圖返回提交,但所有其他提交都存在錯誤。 我不確定它是否與數據庫有關。
我的項目可以在這里找到(我推送了最后一次提交)。 我不確定問題出在哪里以及我可以復制什么以進行設計。 我在這里放了一些可能會顯示問題的代碼。
我能夠使用以下命令通過 rails 控制台創建用戶:
u = User.new(:email => "user@name.com", :username => "test", :password => 'password', :password_confirmation => 'password')
u.save
嘗試注冊時的日志:
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WQkgKxF8rIB1wAq8vnz4Y0bCv9Txlyv0eO8IyEmpEAk=", "user"=>{"email"=>"user@example.com", "username"=>"test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: email
用戶名
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :provide, :uid, :name, :email, :password, :password_confirmation, :remember_me, :username
validates_presence_of :username
has_many :posts
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
模式文件
ActiveRecord::Schema.define(:version => 20131118165834) do
create_table "photo_posts", :force => true do |t|
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end
create_table "posts", :force => true do |t|
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "content_type"
t.integer "content_id"
end
add_index "posts", ["content_type", "content_id"], :name => "index_posts_on_content_type_and_content_id"
add_index "posts", ["user_id"], :name => "index_posts_on_user_id"
create_table "title_posts", :force => true do |t|
t.string "body"
end
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
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"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
t.string "provider"
t.string "uid"
t.string "name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
日志:
Started POST "/users" for 127.0.0.1 at 2013-11-24 00:23:12 +0000
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"awAC+Cn3qQgv2kMwZOlH8Zo60BuV4T41OnKjgvKeytE=", "user"=>{"email"=>"user@example.com", "username"=>"stttt", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameters: email
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35m (0.0ms)[0m rollback transaction
Rendered devise/shared/_links.erb (1.0ms)
Rendered devise/registrations/new.html.erb within layouts/application (15.0ms)
Rendered layouts/_header.html.erb (4.0ms)
Rendered layouts/_footer.html.erb (0.0ms)
Completed 200 OK in 178.0ms (Views: 72.0ms | ActiveRecord: 0.0ms)
發現錯誤是什么。 我使用強參數,這會導致錯誤。 更多信息請訪問https://github.com/plataformatec/devise#strong-parameters
所以解決方案是將其添加到您的 application_controller.rb
# Rails 3.x.x and older
before_filter :configure_permitted_parameters, if: :devise_controller?
# Rails 4.x.x and newer
before_action :configure_permitted_parameters, if: :devise_controller?
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end
請記住為每個操作進行配置,例如 :sign_in、:sign_up、:account_update 等。
上面的答案現在對於 Devise 4 和 Rails 5 來說是不同的。
According to the documentation:
Devise 4 的 Parameter Sanitaizer API 已更改
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
end
end
Rails 5,未定義的方法 `for' for #<Devise on line devise_parameter_sanitizer.for
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.