设计错误:电子邮件不能为空 [英] Devise error: email can't be blank

查看:145
本文介绍了设计错误:电子邮件不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(编辑:添加的development.log日志,未经许可的参数:电子邮件)



Devise正常工作,但我使用rails控制台删除所有用户,并尝试使一个新的用户。我收到一个错误,电子邮件不能为空白。当我删除:validatable block我得到这个错误



我尝试返回提交,但所有其他提交都存在错误。我不知道它是否与数据库有关。



我的项目可以找到这里(我推最后一个提交)。我不知道问题在哪里,我可以复制的设计。我已经把一些代码放在这里可能会出现问题。



我可以通过rails控制台创建一个用户:

  u = User.new(:email =>user@name.com,:username =>test,:password =>'password' :password_confirmation =>'password')
u.save

尝试注册时记录:

 处理由Devise :: RegistrationsController#创建为HTML 
参数:{utf8=>✓ ,authenticity_token=>WQkgKxF8rIB1wAq8vnz4Y0bCv9Txlyv0eO8IyEmpEAk =,user=> {email=>user@example.com,username=>test,password=> [FILTERED],password_confirmation=>[FILTERED]},commit=>注册}
未经许可的参数:电子邮件
/ pre>

User.rb

  class User< ActiveRecord :: Base 


devise:database_authenticatable,:可注册,:omniauthable,
:可恢复,可记忆,可追踪,可验证

attr_accessible :提供,:uid,:name,:email,:password,:password_confirmation,:remember_me,:username

validates_presence_of:username
has_many:posts

def self.from_omniauth(auth)
其中(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)
如果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?
超级&& provider.blank?
end

def update_with_password(params,* options)
如果encrypted_pa​​ssword.blank?
update_attributes(params,* options)
else
super
end
end
end

schema.rb

  ActiveRecord :: Schema.define( version => 20131118165834)do 

create_tablephoto_posts,:force => true do | t |
t.stringimage_file_name
t.stringimage_content_type
t.integerimage_file_size
t.datetimeimage_updated_at
end

create_tableposts,:force => true do | t |
t.integeruser_id
t.datetimecreated_at,:null => false
t.datetimeupdated_at,:null => false
t.stringcontent_type
t.integercontent_id
end

add_indexposts,[content_type,content_id], name => index_posts_on_content_type_and_content_id
add_indexposts,[user_id],:name => index_posts_on_user_id

create_tabletitle_posts,:force => true do | t |
t.stringbody
end

create_tableusers,:force => true do | t |
t.stringemail,:default => ,:null => false
t.stringencrypted_pa​​ssword,:default => ,:null => false
t.stringreset_password_token
t.datetimereset_password_sent_at
t.datetimeremember_created_at
t.integersign_in_count,,default => 0,:null => false
t.datetimecurrent_sign_in_at
t.datetimelast_sign_in_at
t.stringcurrent_sign_in_ip
t.stringlast_sign_in_ip
t.datetimecreated_at ,:null => false
t.datetimeupdated_at,:null => false
t.stringusername
t.stringprovider
t.stringuid
t.stringname
end

add_indexusers,[email],:name => index_users_on_email,:unique => true
add_indexusers,[reset_password_token],:name => index_users_on_reset_password_token,:unique => true

end

日志



 在2013-11-24 00:23:12启动POST/ users为127.0.0.1 +0000 
处理参数:{utf8=>✓,authenticity_token=>awAC + Cn3qQgv2kMwZOlH8Zo60BuV4T41OnKjgvKeytE =,user=> {email= >user@example.com,username="stttt,password=>[FILTERED],password_confirmation=>[FILTERED]},commit=> ;注册}
未经允许的参数:电子邮件
[1m [36m(0.0ms)[0m [1mbegin transaction [0m
[1m [35m(0.0ms)] [0m rollback transaction
渲染设计/共享/ _links.erb(1.0ms)
布局/应用程序内的渲染设计/注册/ new.html.erb(15.0ms)
渲染布局/ _header.html.erb 4.0ms)
渲染布局/ _footer.html.erb(0.0ms)
在178.0ms完成200 OK(视图:72.0ms | ActiveRecord:0.0ms)


解决方案

发现错误是什么。我使用强大的参数,导致错误。更多 https://github.com/plataformatec/devise#strong-parameters



所以解决方案是将它添加到你的application_controller.rb中

  #Rails 3.xx及以上
before_filter:configure_permitted_pa​​rameters,如果::devise_controller?

#Rails 4.x.x和更新
before_action:configure_permitted_pa​​rameters,如果::devise_controller?

def configure_permitted_pa​​rameters
devise_parameter_sanitizer.for(:sign_up){| u | u.permit(:username,:email,:password,,password_confirmation)}
end

请记住为每个动作配置:sign_in,sign_up,account_update等。


(Edit: added development.log logs, "Unpermitted parameters: email")

Devise was working fine but I deleted all users using rails console and tried to make a new user. I get an error that email can't be blank. When I remove the :validatable block I get this error.

I tried to go back to commits but the error exists on all other commits. I am not sure if its something to do with the database.

My project can be found here (I pushed the last commit). I am not sure where the problem is and what I can copy for devise. I have put some code here which may show the problem.

I was able to create a user through rails console using:

u = User.new(:email => "user@name.com", :username => "test", :password => 'password', :password_confirmation => 'password')
u.save

Logs when trying to signup:

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

User.rb

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

schema.rb

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

logs:

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)

解决方案

Found what the error is. I use strong parameters and that causes an error. More at https://github.com/plataformatec/devise#strong-parameters

So the solution is to add this into your 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

Keep in mind to configure for each action like :sign_in, :sign_up, :account_update etc.

这篇关于设计错误:电子邮件不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆