undefined method `premium?' for nil:NilClass

I am getting the following error when I try to click on the wiki show view of my application, but only when I am not signed in as a user (and I want the wiki show view to be public), and I cannot figure out why (it is showing the error at <% if current_user.premium? %>) :

NoMethodError in Wikis#show Showing /home/vagrant/code/Blocipedia/app/views/wikis/show.html.erb where line #11 raised: undefined method `premium?' for nil:NilClass

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

app/views/wikis/show.html.erb:11:in `_app_views_wikis_show_html_erb___543246191_90269080'

Here is the same error from my server logs:

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)

Here is my wiki show view file:

<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>

Here is my application layout file:

<!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>

Here is my wiki controller:

    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

Thanks in advance for your help!


ANSWERS:


When the user isn't signed in, current_user is nil. So the line you highlighted is the same thing as if nil.premium?, and of course nil doesn't have a premium? method attached to it.

I would change the conditional to if current_user && current_user.premium?.



 MORE:


 ? undefined method `default_image' for nil:NilClass
 ? C# WinForms - Determine when void method returns
 ? C# - Unable to add item to listview from method invoked from class
 ? GCC : Return statement from a void function in C
 ? How to call objective c method from c method iOS
 ? Creating multiple choice for user
 ? Does "return;" at the end of a void method in Java have ANY impact on performance?
 ? Java If else nested. public static void ()
 ? FETCH API return undefined
 ? PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"