Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise error: email can't be blank

(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 => "[email protected]", :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"=>"[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

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"=>"[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)
like image 358
Stefanos.Ioannou Avatar asked Nov 21 '13 16:11

Stefanos.Ioannou


1 Answers

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.

like image 125
Stefanos.Ioannou Avatar answered Sep 30 '22 16:09

Stefanos.Ioannou