2013-11-21 39 views
4

(编辑:添加development.log日志,“未经许可的参数:电子邮件”)设计错误:邮件不能为空

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

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

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

我能够创建使用通过铁轨控制台的用户:

u = User.new(:email => "[email protected]", :username => "test", :password => 'password', :password_confirmation => 'password') 
u.save 

日志尝试注册时:

Processing by Devise::RegistrationsController#create as HTML 
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WQkgKxF8rIB1wAq8vnz4Y0bCv9Txlyv0eO8IyEmpEAk=", "user"=>{"email"=>"[email protected]", "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"=>"[email protected]", "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) 
+0

是如何你试图添加一个新用户?在轨道控制台?你这样做的时候是否添加了电子邮件? –

+0

编号通过应用程序。我使用rails控制台只是为了添加一个用户名,看看我能否这样做。但是,在使用Devise的应用程序中,我无法注册用户。 –

+0

但是您在注册过程中是否包含电子邮件? –

回答

9

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

+0

这是完美的。谢谢... –

相关问题