title: Rails Exception: Action Controller sort_by nil:NilClass when attempting to error handle
body: <p>I am a bit new to Ruby on Rails (and already loving it) and attempting to make a very simple webapp that lists posts with the submit form and the 'feed' on one page. I got a lot of these programming concepts from Michael Hartl's wonderful Rails Tutorial. </p>
  
  <p>Posting works fine; the error is thrown when the form is submitted with no form data (to test error handling).</p>
  
  <p>Sorry for the newbie question, but I'm really trying to learn and your help is greatly appreciated. </p>
  
  <p>I've searched over and over again for these exceptions and I can't seem to trace it. I think that the problem lies on the <code>@posts = Post.all</code> line, and I think that it's pulling the null post that wasn't saved, but that's the only conclusion I can come to, and the way to fix it isn't apparent to me.</p>
  
  <p>If you need any more code, I'm happy to post it, and thanks so much for your help!</p>
  
  <p>Here is the offending code that throws the error:</p>
  
  <p><strong>/app/controllers/posts_controller.rb</strong></p>
  
  <pre><code>class PostsController &lt; ApplicationController
    def index
      @posts = Post.all
      @post = Post.new
    end
  
    def create
      @post = Post.new(params[:post])
      if @post.save
        redirect_to '/', :flash =&gt; { :success =&gt; "Awesomesauce! Your post has been broadcast (like a boss)!" }
      else
        @post.destroy
        render 'index'
      end
    end
  
    def destroy
      @post = Post.destroy
    end
  
  end
  </code></pre>
  
  <p><strong>/app/models/post.rb</strong></p>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    attr_accessible :author, :location, :text
  
    validates :author, presence: true
    validates :text, presence: true, length: {maximum: 140}
  
  
  end
  </code></pre>
  
  <p><strong>index.html.erb</strong> (I truncated some irrelevant code here)</p>
  
  <pre><code>&lt;div class="span8 center"&gt;
          &lt;%= render partial: "shared/flash_messages", flash: flash %&gt;
          &lt;%= render 'shared/post_form' %&gt;
          &lt;h4&gt;Latest posts &lt;span class="label label-info"&gt;Updated LIVE&lt;/span&gt;&lt;/h4&gt;
          &lt;ol class="posts"&gt;
              &lt;% @posts.sort_by{|t| - t.created_at.to_i}.each do |post|%&gt;
              &lt;li&gt;
                  &lt;span class="content"&gt; &lt;%= post.text %&gt; &lt;/span&gt;
                  &lt;span class="content muted"&gt;Posted &lt;%= time_ago_in_words(post.created_at) %&gt; ago by &lt;%= post.author %&gt; in &lt;%= post.location %&gt;. &lt;/span&gt;
              &lt;/li&gt;
              &lt;% end %&gt;
  </code></pre>
  
  <p><strong>/config/routes.rb</strong></p>
  
  <pre><code>match '/', to: 'posts#index'
    resources :posts
  </code></pre>
  
  <p>The error that is thrown is as follows:</p>
  
  <pre><code>undefined method `sort_by' for nil:NilClass
  Extracted source (around line #14):
  
  11:         &lt;%= render 'shared/post_form' %&gt;
  12:         &lt;h4&gt;Latest posts &lt;span class="label label-info"&gt;Updated LIVE&lt;/span&gt;&lt;/h4&gt;
  13:         &lt;ol class="posts"&gt;
  14:             &lt;% @posts.sort_by{|t| - t.created_at.to_i}.each do |post|%&gt;
  15:             &lt;li&gt;
  16:                 &lt;span class="content"&gt; &lt;%= post.text %&gt; &lt;/span&gt;
  17:                 &lt;span class="content muted"&gt;Posted &lt;%= time_ago_in_words(post.created_at) %&gt; ago by &lt;%= post.author %&gt; in &lt;%= post.location %&gt;. &lt;/span&gt;
  </code></pre>