设计错误:电子邮件不能为空

时间:2013-11-21 16:11:05

标签: ruby-on-rails ruby-on-rails-3 devise

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

Devise工作正常,但我删除了所有使用rails控制台的用户,并试图建立一个新用户。我收到email can't be blank的错误。当我删除:validatable块时,我会收到error

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

我的项目可以找到here(我推了最后一次提交)。我不确定问题出在哪里以及我可以为设计复制什么。我在这里放了一些可能显示问题的代码。

我能够通过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

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

日志

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)

2 个答案:

答案 0 :(得分:10)

发现错误是什么。我使用强参数,这会导致错误。更多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等。

答案 1 :(得分:0)

以上答案现在对于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, Undefined method `for' for #<Devise on line devise_parameter_sanitizer.for