这是我的登录应用程序。请任何人都可以解释我如何查看登录验证
pbl01 /配置/ routes.rb中
Rails.application.routes.draw do
devise_for :users, :controllers => {
:registrations => 'users/registrations'
}
resources :import,only: [:index,:create]
root 'root#index'
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
end
pbl01 /应用/控制器/用户/ sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
before_action :configure_sign_in_params, only: [:create]
def new
end
def create
authorized_user=User.authenticate(params[:userid],params[:password])
if authorized_user
flash[:notice] = "Wow Welcome again, you logged in as"
else
flash[:notice] = "Invalid Username or Password"
flash[:color]= "invalid"
render "new"
end
end
pbl01 /应用/模型/ user.rb
require 'csv'
class User < ApplicationRecord
validates :userid, :presence => true,
validates :password, :presence => true,
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def email_required?
false
end
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
users = find_by(userid: row["userid"]) || new
users.attributes = row.to_hash.slice(*updatable_attributes)
users.password= row["password"]
users.save!
end
end
def self.updatable_attributes
["userid", "name", "email"]
end
end
pbl01 /应用/视图/设计/会话/ new.html.erb
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<% if @user.errors.any? %>
<div class="field">
<%= f.label :userid %><br />
<%= f.text_field :userid, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
<% for message_error in @user.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
答案 0 :(得分:0)
I believe you should change to something like this.
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url:
session_path(resource_name)) do |f| %>
<% if @user.errors.any? %>
<% for message_error in @user.errors.full_messages %>
<li>* <%= message_error %></li>
<% end %>
<% end %>
<div class="field">
<%= f.label :userid %><br />
<%= f.text_field :userid, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end %>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>