title: Rails noob: getting value from database and showing on application.html.erb
body: <p><strong>brief update</strong>
  I might not even bother with a form to submit the data to the database and instead just use Heroku console, so my only concern is retrieving it. </p>
  
  <p><strong>Original Question</strong></p>
  
  <p>I've read all the books but now that I'm trying to implement I'm getting confused</p>
  
  <p>I created a model named Total (not the best name, but that's another problem) with three columns, <code>profit</code>, <code>number</code> and <code>fees</code>. Here's the console printout</p>
  
  <pre><code>t = Total.first
   =&gt; #&lt;Total id: 1, profit: 500, number: 7, fees: 40, created_at: "2012-01-12 04:21:33", updated_at: "2012-01-12 04:21:58"&gt; 
  </code></pre>
  
  <p>(Note, I assume that this entry will be available to present at localhost:3000 if made on the development database)</p>
  
  <p>The easiest place for me to present this data (due to my webpage layout) is in <code>application.html.erb</code> so I've got a little table like this. When you look at this table, you can see how I wish to present the data by calling 'profit' on an instance variable <code>@t.profit</code> <code>@t.number</code> and <code>@t.fees</code>.</p>
  
  <pre><code>&lt;table class = "condensed-table" &gt;
          &lt;thead&gt;
            &lt;tr&gt;
  
              &lt;th&gt;Profit &lt;/th&gt;
              &lt;th&gt;Number of Trades&lt;/th&gt;
              &lt;th&gt; Fees&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
            &lt;tr&gt;
  
  
              &lt;td&gt;$&lt;%= @t.profit  %&gt; &lt;/td&gt;
              &lt;td&gt;&lt;%=  @t.number %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%=  @t.fees %&gt;&lt;/td&gt;
            &lt;/tr&gt;
            &lt;/tbody&gt;
        &lt;/table&gt;
  </code></pre>
  
  <p>So now it's only a question of getting the data from the database, and creating the instance variable so I can call @t.profit, just like I did in the console</p>
  
  <pre><code>ruby-1.9.2-p290 :019 &gt; t.profit
   =&gt; 500 
  </code></pre>
  
  <p>So with a <code>Total.rb model</code> and presentation in <code>application.html.erb</code>, I figured I needed to do this ....</p>
  
  <pre><code>@t = Total.find(params[:id])
  </code></pre>
  
  <p>to get the data from the database, but what should I call the method and where should i put it to make it work? </p>
  
  <p>For example, if I had a pages controller and a pages view, I could just create an index action in the view like </p>
  
  <pre><code>def index
  @t = Total.find(params[:id])
  end
  </code></pre>
  
  <p>and then I could just do <code>@t.profit</code> (i assume). However, if I did something like that my table to present the data wouldn't be available on every part of my site.</p>
  
  <p>I tried to put it in application_controller.rb</p>
  
  <pre><code> def index
  
      @t = Total.find(params[:id])
  
    end
  </code></pre>
  
  <p>and got this error message</p>
  
  <pre><code>NoMethodError in Posts#index
  
  Showing /Users/me/Sites/enki2/app/views/layouts/application.html.erb where line #73 raised:
  
  undefined method `profit' for nil:NilClass
  
  Extracted source (around line #73):
  </code></pre>