title: NoMethodError in Events#show | undefined method `firstname' for nil:NilClass
body: <p>i'm very new to ruby on rails and i apologise in advance if my below question is very basic, any help would be much appreciated. </p>
  
  <ol>
  <li><p>Following the "railscast #154 Polymorphic Association" i am now able to create comments for an event and a blog, </p></li>
  <li><p>but what i would like also to do is, when a user post a comment the name of the user (who posted the comment) appears with the comment they posted....something like the below</p></li>
  <li><p>As below in details - In my views/comments/_comments.html.erb file i placed <strong>&lt;%=comment.user.firstname%></strong> &amp; controllers/comments_controller.rb in the index method i added <strong>@comments.user = current_user</strong>, but could not get it to display the current_user's name with the comment they posted as below</p></li>
  </ol>
  
  <blockquote>
    <p><strong>emma thompson</strong> great event, thoroughly enjoyed it!</p>
    
    <p>emma thompson being the current_user</p>
    
    <p>great event, thoroughly enjoyed it! being the comment</p>
  </blockquote>
  
  <p><strong>- QUESTION: How do i call up the name of the user who posted the comment?</strong></p>
  
  <p><strong>error message</strong></p>
  
  <pre><code>NoMethodError in Events#show
  Showing /Users/ARTLoe/00_GitH/00_Projects/spefz_app/app/views/comments/_comments.html.erb where line #5 raised:
  undefined method `firstname' for nil:NilClass
  
      &lt;ul&gt;
      &lt;% @comments.each do |comment| %&gt;
        &lt;li&gt;
          &lt;%= comment.user.firstname %&gt;          #&lt;--------------error appears on this line
          &lt;%= simple_format comment.content %&gt;
        &lt;/li&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p><strong>schema</strong></p>
  
  <pre><code>  create_table "comments", force: true do |t|
      t.text     "content"
      t.integer  "commentable_id"
      t.string   "commentable_type"
      t.integer  "user_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  
    add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree
  
    create_table "events", force: true do |t|
      t.string   "name"
      t.text     "description"
      t.date     "date"
      t.time     "time"
      t.text     "city"
      t.decimal  "price",       precision: 8, scale: 2
      t.datetime "created_at"
      t.datetime "updated_at"
      t.integer  "user_id"
    end
  
    create_table "users", force: true do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.string   "firstname"
      t.string   "lastname"
      t.date     "dob"
      t.string   "gender"
      t.text     "description"
      t.string   "role"
    end
  </code></pre>
  
  <p><strong>models</strong></p>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :commentable, polymorphic: true
    belongs_to :user
  end
  
  class Event &lt; ActiveRecord::Base
    belongs_to :user
    has_many :comments, as: :commentable
  end
  
  class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable  
    has_many :events
    has_many :comments, as: :commentable
  end
  </code></pre>
  
  <p><strong>controllers</strong></p>
  
  <pre><code>#&lt;&lt;&lt;COMMENT CONTROLLER
  class CommentsController &lt; ApplicationController
    before_action :set_comment, only: [:show, :edit, :update, :destroy]
    before_filter :load_commentable
  
    def index
      @comments = @commentable.comments
      @comments.user = current_user
    end
  
    def show
    end
  
    def new
      @comment = @commentable.comments.new
    end
  
    def edit
    end
  
    def create
      @comment = @commentable.comments.new(comment_params)
  
      respond_to do |format|
        if @comment.save
          format.html { redirect_to [@commentable], notice: 'Comment was successfully created.' }
          format.json { render :show, status: :created, location: @comment }
        else
          format.html { render :new }
          format.json { render json: @comment.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def update
      respond_to do |format|
        if @comment.update(comment_params)
          format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
          format.json { render :show, status: :ok, location: @comment }
        else
          format.html { render :edit }
          format.json { render json: @comment.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def destroy
      @comment.destroy
      respond_to do |format|
        format.html { redirect_to events_url, notice: 'Comment was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_comment
        @comment = Comment.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def comment_params
        params.require(:comment).permit(:content, :user_id)
      end
  
      def load_commentable
        resource, id = request.path.split('/')[1, 2]
        @commentable = resource.singularize.classify.constantize.find(id)
      end
  end
  
  
  #&lt;&lt;&lt;EVENT CONTROLLER
  class EventsController &lt; ApplicationController
    before_action :set_event, only: [:show, :edit, :update, :destroy]
    before_filter :authenticate_user!
  
    def index
      @events = Event.order(:date)
      # @events = current_user.events | displays only events by current user
    end
  
    def show
      @commentable = @event
      @comments = @commentable.comments
      @comment = Comment.new
    end
  
    def new
      @event = Event.new
    end
  
    def edit
    end
  
    def create
      @event = Event.new(event_params)
  
      respond_to do |format|
        if @event.save
          format.html { redirect_to @event, notice: 'Event was successfully created.' }
          format.json { render :show, status: :created, location: @event }
        else
          format.html { render :new }
          format.json { render json: @event.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def update
      respond_to do |format|
        if @event.update(event_params)
          format.html { redirect_to @event, notice: 'Event was successfully updated.' }
          format.json { render :show, status: :ok, location: @event }
        else
          format.html { render :edit }
          format.json { render json: @event.errors, status: :unprocessable_entity }
        end
      end
    end
  
    def destroy
      @event.destroy
      respond_to do |format|
        format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      def set_event
        @event = Event.find(params[:id])
      end
  
      def event_params
        params.require(:event).permit(:name, :description, :date, :time, :city, :price)
      end
  end
  </code></pre>
  
  <p><strong>views</strong> i am rendering the comment partials in views/events/show.html.erb
  in the _comments.html.erb i typed <strong>&lt;%= comment.user.firstname %></strong> but received an error</p>
  
  <pre><code>#&lt;&lt;&lt;VIEWS/COMMENTS/_COMMENTS.HTML.ERB
  &lt;div&gt;
    &lt;ul&gt;
    &lt;% @comments.each do |comment| %&gt;
      &lt;li&gt;
        &lt;%= comment.user.firstname %&gt;
        &lt;%= simple_format comment.content %&gt;
      &lt;/li&gt;
    &lt;% end %&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  
  #&lt;&lt;&lt;VIEWS/COMMENTS/_FORM.HTML.ERB
  &lt;%= form_for [@commentable, @comment] do |f| %&gt;
    &lt;% if @comment.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@comment.errors.count, "error") %&gt; prohibited this comment from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @comment.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.text_area :content, rows: 8 %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit "Post" %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>