我有一个登录页面,其中包含一个表单,用户可以在其中输入电子邮件和密码。我检查电子邮件和密码是否与活动记录中的条目相匹配,并且如果发出通知,我会说“ Signed In”(登录),默认情况下它是绿色的,但是我还有关于无效登录的另一条通知,我在处理方法上遇到麻烦使该通知变为红色而不是绿色。
这是我的登录index.html.erb
<p id="notice"><%= notice %></p>
<h1>Please Login</h1>
<%= form_for(:user, :url => { :action => "login" }) do |form| %>
<%= form.text_field :email, :placeholder => 'Email'%>
<br><br>
<%= form.password_field :password, :placeholder => 'Password'%>
<br><br>
<%= form.submit 'Login' %>
<%= link_to "Cancel", home_index_path %>
<% end %>
这是我的login_controller.rb
class LoginController < ApplicationController
def index
end
def login
email = params[:user][:email]
password = params[:user][:password]
@user = User.find_by(email: email, password: password)
respond_to do |format|
if @user.try(:id)
format.html { redirect_to login_index_path, notice: 'Signed In'}
else
format.html { redirect_to login_index_path, notice: 'Invalid username/password combination' }
end
end
end
end
答案 0 :(得分:1)
只需更改您的控制器即可
respond_to do |format|
if @user
format.html { redirect_to login_index_path, notice: 'Signed In'}
else
format.html { redirect_to login_index_path, alert: 'Invalid username/password combination' }
end
end
在找不到用户的情况下将键notice
更改为alert
会改变颜色:D