当我尝试通过输入错误的数据(通常应显示登录失败的消息)时,我的登录表单中出现以下错误。
错误:
NoMethodError in Sessions#loginadmin
Showing C:/Site/swargadwar_admin/app/views/homes/index.html.erb where line #71 raised:
undefined method `errors' for nil:NilClass
Extracted source (around line #71):
68: <% end %>
69: </div>
70: <div class="error-div">
71: <% if @admin.errors.any? %>
72: <div id="error_explanation">
73: <h2><%= pluralize(@admin.errors.count, "error") %> prohibited this post from being saved:</h2>
74:
请检查下面的代码,让我知道我在做错误的地方。
<div class="container">
<div style="text-align:center;"><img src="/assets/admin.png" style="width:100px; height:120px; " /></div>
<div class="text-div" style="text-align:center;">Swargadwar, Puri Municipality,govt of odisha</div>
<section>
<% if !current_user %>
<div id="container_demo" >
<!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4 -->
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="login" class="animate form">
<%= form_for :admin,:url => {:action =>'loginadmin',:controller => 'sessions' } do |f| %>
<h1>Log in</h1>
<p>
<label for="username" class="uname" data-icon="u" > Your email or username </label>
<%= f.email_field :email,placeholder:"mysupermail@mail.com",:id => "username" %>
</p>
<p>
<label for="password" class="youpasswd" data-icon="p"> Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "password" %>
</p>
<p class="keeplogin">
<%= f.check_box :remember_me,:id => "loginkeeping" %>
<label for="loginkeeping">Keep me logged in</label>
<%= link_to 'Forgetting password ?',admins_forget_path %>
</p>
<p class="login button">
<%= f.submit "Login" %>
</p>
<p class="change_link">
Not a member yet ?
<a href="#toregister" class="to_register">Join us</a>
</p>
<% end %>
</form>
</div>
<div id="register" class="animate form">
<%= form_for :admin,:url => {:action => 'create_registration',:controller => "admins" } do |f| %>
<h1> Sign up </h1>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Your username</label>
<%= f.text_field :user_name,placeholder:"mysuperusername690",:id => "usernamesignup" %>
</p>
<p>
<label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
<%= f.email_field :email,placeholder:"mysupermail@mail.com",:id => "emailsignup" %>
</p>
<p>
<label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
<%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
<%= f.password_field :password_confirmation,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
</p>
<p>
<label for="usernamesignup" class="uname" data-icon="u">Add Image</label>
<%= f.file_field :picture %>
</p>
<p class="signin button">
<%= f.submit "Sign Up"%>
</p>
<p class="change_link">
Already a member ?
<a href="#tologin" class="to_register"> Go and log in </a>
</p>
<% end %>
</div>
<div class="error-div">
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
</section>
</div>
class SessionsController < ApplicationController
def loginadmin
@admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
if @admin
session[:user_id]=@admin.id
cookies.signed[:user_id]=@admin.id
params[:admin][:remember_me] == '1' ? remember(@admin) : forget(@admin)
flash[:notice]="Login Successfull"
flash[:color]="valid"
redirect_to :action => "new", :controller => "admins"
else
flash[:notice]="Login Failed"
flash[:color]="invalid"
render 'homes/index'
end
end
def removeuser
session[:user_id] = nil
cookies.delete :user_id
flash[:notice]="user logged out successfully"
flash[:color]="valid"
redirect_to :action => 'index', :controller => 'homes'
end
end
class HomesController < ApplicationController
def index
@admin=Admin.new
end
end
请帮我解决此错误。
答案 0 :(得分:0)
错误是不言自明的。
Sessions中的NoMethodError#loginadmin
nil的未定义方法`errors':NilClass
您收到此错误是因为 @admin为nil ,并且您在
如果您查看loginadmin操作是会话控制器,则在没有@admin对象时呈现'index'模板,这会导致此错误。
<强>修正:强>
使用部分中的公共代码为这些操作创建单独的模板,或者只使用rails try method
返回nil而不是引发异常
<% if @admin.try(:errors).try(:any?) %>
OR
只需在顶部使用条件即可找出错误代码:
<% if @admin %>
<div class="error-div">
<% if @admin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@admin.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @admin.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
<% end %>
P.S 我建议您使用不同的模板,将常用代码分成部分。