2012-06-04 28 views
0

問題解決Rails的3&設計:當註銷

klaustopher他的回答解決了這個問題的錯誤。


的問題是,當我有<%,我不能退出= current_user.email%>或<%= current_user.username $>在我的佈局/ application.html.erb

當我從我的application.html.erb中刪除了這一行,我可以無誤地註銷。 我在谷歌搜索,但找不到這個問題的任何事情。

我application.html.erb:

<!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荷蘭話

回答

1

當你已註銷,當前用戶將返回nil。在nil調用的方法會返回一個錯誤

1.9.3p125 :002 > nil.username 
NoMethodError: undefined method `username' for nil:NilClass 

你必須檢查是否CURRENT_USER返回比無其他東西。你可以通過使用

<%= current_user.username if current_user %> 
+0

謝謝,作爲初學者,這對我來說有點棘手。我希望其他人對此主題有所瞭解。 –

1

你需要檢查CURRENT_USER的存在如: <%if current_user.present? %> <%= current_user.email%>或<%= current_user.username%> <%端%>

+0

這也是對的,但klaustopher他的答案是更容易閱讀的鐵軌新手。 –