首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3& Devise:注销时出错

Rails 3& Devise:注销时出错
EN

Stack Overflow用户
提问于 2012-06-04 13:53:07
回答 2查看 205关注 0票数 0

问题求解

他的回答解决了这个问题。

问题是,当我的布局/application.html.erb中有<%= current_user.email %>或<%= current_user.username$>时,我无法注销。

当我从我的application.html.erb中删除这一行时,我可以在没有错误的情况下退出。我在google上搜索过,但是没有找到关于这个问题的任何东西。

我的application.html.erb:

代码语言:javascript
复制
<!DOCTYPE html>
<html id="html">

<head>
<title><%= content_for?(:title) ? yield(:title) : "Contractbeheersysteem" %></title>
    <%= stylesheet_link_tag "application", "simple_form", "gegevens", "drop-down-menu",      "table", :media => "all" %>
    <%= javascript_include_tag "autocomplete-rails.js" %>
    <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "application" %>
    <%= javascript_include_tag 'jquery.dataTables.min' %>
    <%= csrf_meta_tag %>
    <%= yield(:head) %>
</head>

<body>

<body link="#999" vlink="black" alink="black">  

<div id="menu">
    <ul id="drop-down-menu">
    <li><%= link_to "Home Page", home_index_path, :class => current_page?(home_index_path) ? "current" : "" %></li> 
    <li><%= link_to "Bedrijfsgegevens", bedrijfsgegevens_path, :id => 'bednav' %>
    <ul>
        <li><%= link_to "Nieuw Bedrijf toevoegen", new_bedrijfsgegeven_path %></li>
    </ul> 
    </li> 
    <li><%= link_to "Contactpersonen", contactpersoons_path, :id => 'contanav' %>
    <ul> 
        <li><%= link_to "Nieuw Contactpersoon", new_contactpersoon_path %></li> 
    </ul> 
    </li>
    <li><%= link_to "Contractgegevens", contractgegevens_path(@contractgegevens), :id => 'contrnav' %>
    <ul> 
        <li><%= link_to "Nieuwe Contractgegevens", new_contractgegeven_path %></li> 
    </ul>
        <li><%= link_to "Contactformulier", contact_index_path, :id => 'formnav' %>        </li>
    <% if user_signed_in? %>
        <li><%= link_to "My Account", users_path(@users) %>
        <ul>
            <li><%= link_to "Uitloggen", destroy_user_session_path, :method => :delete %>
            <li><%= link_to "Gebruikers", user_registration_path %>
        </ul>
    <% else %>
        <li><%= link_to "Inloggen", new_user_session_path %>
        <ul>
            <li><%= link_to "Registreren", new_user_registration_path %>
        </ul>
    <% end %>
    </ul>   
  <p class="clear_all"></p>
</div>

<div id="login">
Inlogd als: <%= current_user.username %>
</div>

<div id="content">
  <hr>
    <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, :id => "flash_#{name}" %>
    <% end %>
    <%= yield %>
 </div>

 <div id="footer">
    <hr color="#999", width="100%">
    <h4>
    <b><a href="http://piterjelles.nl">Piter Jelles</a> &copy; 2012 </b> | <a href="http://rubyonrails.org">Ruby on Rails </a> 
    </h4>
 </div>

 </font>    
 </body>
 </div>
 </html>

注:我来自荷兰,所以在我的application.html.erb中有荷兰语

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-04 13:58:11

注销后,当前用户将返回nil。在nil上调用方法时会出现错误

代码语言:javascript
复制
1.9.3p125 :002 >   nil.username
NoMethodError: undefined method `username' for nil:NilClass

您必须检查current_user是否返回除零以外的其他内容。您可以通过使用

代码语言:javascript
复制
<%= current_user.username if current_user %>
票数 1
EN

Stack Overflow用户

发布于 2012-06-04 13:56:39

您需要检查current_user的存在,例如:<% if current_user.present?%> <%= current_user.email %>或<%= current_user.username %> <% end %>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10882517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档