感谢阅读! 问题似乎很容易解决,但我与它战斗4小时,所以: 机型:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
同样适用于Pupil和Teacher课程,但是课程Pupil&lt; ActiveRecord :: Base等。
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_pupil!
before_action :authenticate_admin!
before_action :authenticate_teacher!
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>EDziennik2</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<p class="navbar-text pull-right">
<% if admin_signed_in? %>
Logged in as <strong><%= current_admin.email %></strong>.
<br>
<%= current_admin %>
<%= link_to 'Edit profile', edit_admin_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_admin_session_path, method: :delete, :class => 'navbar-link' %>
<% elsif pupil_signed_in? %>
Logged in as <strong><%= current_pupil.email %></strong>.
<br>
<%= current_pupil %>
<%= link_to 'Edit profile', edit_pupil_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_pupil_session_path, method: :delete, :class => 'navbar-link' %>
<% elsif teacher_signed_in? %>
Logged in as <strong><%= current_teacher.email %></strong>.
<br>
<%= current_teacher %>
<%= link_to 'Edit profile', edit_teacher_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_teacher_session_path, method: :delete, :class => 'navbar-link' %>
<% end %>
<%= yield %>
</body>
</html>
问题是,当我尝试登录时,我需要传递3次电子邮件和密码。然后我正确登录。对我来说,这是application_controller.rb和3x before_action身份验证行的问题,因为当我删除其中2个时,我正确登录。任何想法该怎么做?
修改 好的,我想我已经解决了这个问题:) 在application_controller.rb中,我定义了一个名为
的新方法认证!
def authenticate!
if @current_user == current_admin
:authenticate_admin!
elsif @current_user == current_pupil
:authenticate_pupil
elsif @current_user == current_teacher
:authenticate_teacher
end
end