title: undefined method `each' for nil:NilClass upon integration test
body: <p>I'm developing a web app using Ruby on Rails and since I'm still pretty new to it, I forgot that I'm suppose to write test before my application code and don't wanna redo it. So before I wrote my test, I've already wrote my application codes and it is all working fine on the browser, but after I wrote my integration tests, I've a whole bunch of <code>ActionView::Template::Error: undefined method</code>each' for nil:NilClass` errors. </p>
  
  <p>I'll copy and paste one error for example and I hope the answers can extrapolate to my other similar errors.  </p>
  
  <p>The error: </p>
  
  <pre><code>ERROR["test_unsuccessful_edit", TransactionEditTest, 0.560911]
   test_unsuccessful_edit#TransactionEditTest (0.56s)
  ActionView::Template::Error:         ActionView::Template::Error: undefined method `each' for nil:NilClass
              app/views/transactions/index.html.erb:4:in `_app_views_transactions_index_html_erb___2730839601929560472_70220029186400'
              app/controllers/transactions_controller.rb:32:in `update'
              test/integration/transaction_edit_test.rb:14:in `block in &lt;class:TransactionEditTest&gt;'
          app/views/transactions/index.html.erb:4:in `_app_views_transactions_index_html_erb___2730839601929560472_70220029186400'
          app/controllers/transactions_controller.rb:32:in `update'
          test/integration/transaction_edit_test.rb:14:in `block in &lt;class:TransactionEditTest&gt;'
  
    16/16: [=================================] 100% Time: 00:00:00, Time: 00:00:00
  
  Finished in 0.59370s
  </code></pre>
  
  <p>Corresponding integration test: </p>
  
  <pre><code>require 'test_helper'
  
  class TransactionEditTest &lt; ActionDispatch::IntegrationTest
  
    def setup
      @transaction = transactions(:test)
    end
  
    test "unsuccessful edit" do
      get edit_transaction_path(@transaction)
      assert_template 'transactions/edit'
      patch transaction_path(@transaction), transaction: { from:  "",
                                      to: "",
                                      amount:              "foo",
                                      description: "bar" }
      assert_template 'transactions/edit'
    end
  </code></pre>
  
  <p>here's my controller: </p>
  
  <pre><code>  class TransactionsController &lt; ApplicationController
    def index
      @transactions = Transaction.all
    end
  
    def show
      @transaction = Transaction.find(params[:id])
    end
  
    def new                                               
      @transaction = Transaction.new
    end
  
    def create                                            
      if @transaction.save
        @transactions = Transaction.all
        render 'index'
      else
        render 'new'
      end
    end
  
    def edit                                            
      @transaction = Transaction.find(params[:id])
    end
  
    def update
      @transaction = Transaction.find(params[:id])
      if @transaction.update_attributes(transaction_params)
        redirect_to(:action =&gt; 'index')                                 
      else
        render('edit')
      end
    end 
  
  
    def destroy
      Transaction.find(params[:id]).destroy
      redirect_to transactions_path
    end
  
    private
  
      def transaction_params
        params.require(:transaction).permit(:from, :to, :amount,
                                     :description)
      end
  end
  </code></pre>
  
  <p>and my index.html.erb</p>
  
  <pre><code>&lt;div class="transaction_list"&gt;
  
  
    &lt;% @transactions.each do |transaction| %&gt;
  
        &lt;li&gt;
          &lt;%= transaction.from %&gt;
          &lt;%= transaction.to %&gt;
          &lt;%= number_with_precision(transaction.amount, :precision =&gt; 2) %&gt;
          &lt;%= transaction.description %&gt;
          &lt;%= link_to "Edit", edit_transaction_path(transaction.id) %&gt; |
          &lt;%= link_to "Delete", transaction, method: :delete, data: { confirm: "Are you sure?" } %&gt;
  
  
        &lt;/li&gt;
    &lt;% end %&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>I know a lot of people had already ask this on stackoverflow but I couldnt find someone with a similar case as mine, I hope someone not only point out my mistake but also explain the concepts to me, thank you very much!!</p>