2014-09-26 46 views
-1

當我嘗試點擊我的應用程序的wiki展示視圖時,出現以下錯誤,但僅當我未以​​用戶身份登錄(並且我希望wiki展示視圖已公開)時,出現以下錯誤,我想不通爲什麼(它顯示錯誤在<% if current_user.premium? %>):未定義的方法`溢價?'對於零:NilClass

NoMethodError在維基#顯示 顯示/home/vagrant/code/Blocipedia/app/views/wikis/show.html.erb在行# 11提出: 未定義的方法'溢價?'對於零:NilClass

 <% if current_user.premium? %> 
    <%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %> 
    <% end %> 
    <p><%= @wiki.body %></p> 

應用程序/視圖/維基/ show.html.erb:11:`_app_views_wikis_show_html_erb___543246191_90269080'

這裏是我的服務器日誌相同的錯誤:

Started GET "/wikis/now-with-title" for 10.0.2.2 at 2014-09-26 14:50:28 +0000 
Processing by WikisController#show as HTML 
    Parameters: {"id"=>"now-with-title"} 
    Wiki Load (1.5ms) SELECT "wikis".* FROM "wikis" WHERE "wikis"."slug" = 'now-with-title' ORDER BY created_at DESC LIMIT 1 
    Rendered wikis/show.html.erb within layouts/application (12.8ms) 
Completed 500 Internal Server Error in 54ms 

ActionView::Template::Error (undefined method `premium?' for nil:NilClass): 
    8:  <%= link_to "Delete Wiki", @wiki, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this wiki?' } %> 
    9:  <% end %> 
    10: 
    11:  <% if current_user.premium? %> 
    12:  <%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %> 
    13:  <% end %> 
    14: <p><%= @wiki.body %></p> 
    app/views/wikis/show.html.erb:11:in `_app_views_wikis_show_html_erb___271164051_80046150' 

4.0.5/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (9.8ms) 

這裏是我的wiki顯示視圖文件:

<div class="row"> 
    <div class="col-md-8"> 

    <h1><%= @wiki.title %></h1> 

    <% if current_user %> 
     <%= link_to "Edit", edit_wiki_path(@wiki), class: 'btn btn-success' %> 
     <%= link_to "Delete Wiki", @wiki, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this wiki?' } %> 
     <% end %> 

     <% if current_user.premium? %> 
     <%= link_to "Add/Remove Collaborators", wiki_collaborators_path(@wiki), class: 'btn btn-info' %> 
     <% end %> 
    <p><%= @wiki.body %></p> 

    </div> 

這裏是我的應用程序佈局文件:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Blocipedia</title> 
    <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 
    <div class="container"> 

    <ul class="nav nav-tabs"> 

     <li><%= link_to "Blocipedia", root_path %></li> 
     <li><%= link_to "About", about_path %></li> 
     <li><%= link_to "Wikis", wikis_path %></li> 



    <div class="pull-right user-info"> 
      <% if current_user %> 
       Hello <%= link_to current_user.email, edit_user_registration_path %>! 
      <%= link_to edit_user_registration_path, class: 'btn btn-primary btn-sm' do %> 
       <span class="glyphicon glyphicon-user"></span> 
      <% end %> 

      <%= link_to destroy_user_session_path, method: :delete, class: 'btn btn-primary btn-sm' do %> 
       <span class="glyphicon glyphicon-log-out"></span> 
       <% end %> 
      <% else %> 
      <%= link_to "Sign In", new_user_session_path %> or 
      <%= link_to "Sign Up", new_user_registration_path %> 
      <% end %> 
     </div> 

    </ul> 

    <% if flash[:notice] %> 
     <div class="alert alert-success"> 
      <button type="button" class="close" data-dismiss="alert">&times;</button> 
      <%= flash[:notice] %> 
     </div> 
     <% elsif flash[:error] %> 
     <div class="alert alert-danger"> 
      <button type="button" class="close" data-dismiss="alert">&times;</button> 
      <%= flash[:error] %> 
     </div> 
     <% elsif flash[:alert] %> 
     <div class="alert alert-warning"> 
      <button type="button" class="close" data-dismiss="alert">&times;</button> 
      <%= flash[:alert] %> 
     </div> 
     <% end %> 

<%= yield %> 

</body> 
</html> 

這裏是我的wiki控制器:

class WikisController < ApplicationController 
     def index 
     @wikis = Wiki.all 
     end 

     def show 
     @wiki = Wiki.friendly.find(params[:id]) 
     end 

     def new 
     @wiki = Wiki.new 
     end 

     def create 
     @wiki = current_user.wikis.build(wiki_params) 

     if @wiki.save 
      flash[:notice] = "Wiki was saved." 
      redirect_to @wiki 
     else 
      flash[:error] = "There was an error saving the wiki. Please try again." 
      render :new 
     end 
     end 

     def edit 
     @wiki = Wiki.friendly.find(params[:id]) 

     end 

     def update 
     @wiki = Wiki.friendly.find(params[:id]) 

     if @wiki.update_attributes(wiki_params) 
      flash[:notice] = "Wiki was updated." 
      redirect_to @wiki 
     else 
      flash[:error] = "There was an error saving the wiki. Please try again." 
      render :edit 
     end 
     end 

     def destroy 
     @wiki = Wiki.friendly.find(params[:id]) 

     title = @wiki.title 

     if @wiki.destroy 
      flash[:notice] = "Wiki was deleted successfully." 
      redirect_to wikis_path 
     else 
      flash[:error] = "There was an error deleting the wiki." 
      render :show 
     end 
     end 

     private 

     def wiki_params 
     params.require(:wiki).permit(:title, :body) 
     end 

end 

預先感謝您的幫助!

回答

1

當用戶未登錄時,current_user爲零。因此,您突出顯示的行與if nil.premium?相同,當然nil沒有附加premium?方法。

我會將條件更改爲if current_user && current_user.premium?

相關問題