I have the problem with devise `admin` model. However with `user` model my app is working fine:
This is my admin model:
class Admin
include Mongoid::Document
devise :database_authenticatable, :trackable, :timeoutable
field :email, :type => String, :default => ""
field :encrypted_password, :type => String, :default => ""
validates_presence_of :email, :role, :password, :password_confirmation
validates_presence_of :encrypted_password
validates_uniqueness_of :email, :case_sensitive => false
## Trackable
field :sign_in_count, :type => Integer, :default => 0
field :current_sign_in_at, :type => Time
field :last_sign_in_at, :type => Time
field :current_sign_in_ip, :type => String
field :last_sign_in_ip, :type => String
#ROLES = %w[admin superadmin]
field :role, :type => String
end
I create a first admin with:
Admin.create! created_at: Time.now, email: "
em...@email.com", password: "11111111", password_confirmation: "11111111"
This is my devise controller `admins/sessions_controller.rb`
class Admins::SessionsController < Devise::SessionsController
#
#
#
end
This is my `route.rb`
MyApp::Application.routes.draw do
devise_for :admins, :controllers => { :sessions => "admins/sessions" }
namespace :admin do
get '', to: 'dashboard#index', as: '/'
end
end
This is my view `admins/sessions/new.html.erb`
<h2>Sign in</h2>
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-inputs">
<%= f.input :email, :required => true %>
<%= f.input :password, :required => true } %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>
<%= f.button :submit, "Sign in" %>
<% end %>
This is my `config/initalizers/strongs_parameters.rb`
Mongoid::Document.send(:include, ActiveModel::ForbiddenAttributesProtection)
When I enter my username and password I can not login on admin panel and I get the error `Invalid email or password`. I get in my log:
**Completed 401 Unauthorized in 1ms**
**Where have I the problem? What am I doing wrong?**