brief update 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.
Original Question
I've read all the books but now that I'm trying to implement I'm getting confused
I created a model named Total (not the best name, but that's another problem) with three columns, profit, number and fees. Here's the console printout
t = Total.first
=> #<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">
(Note, I assume that this entry will be available to present at localhost:3000 if made on the development database)
The easiest place for me to present this data (due to my webpage layout) is in application.html.erb 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 @t.profit @t.number and @t.fees.
<table class = "condensed-table" >
<thead>
<tr>
<th>Profit </th>
<th>Number of Trades</th>
<th> Fees</th>
</tr>
</thead>
<tbody>
<tr>
<td>$<%= @t.profit %> </td>
<td><%= @t.number %></td>
<td><%= @t.fees %></td>
</tr>
</tbody>
</table>
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
ruby-1.9.2-p290 :019 > t.profit
=> 500
So with a Total.rb model and presentation in application.html.erb, I figured I needed to do this ....
@t = Total.find(params[:id])
to get the data from the database, but what should I call the method and where should i put it to make it work?
For example, if I had a pages controller and a pages view, I could just create an index action in the view like
def index
@t = Total.find(params[:id])
end
and then I could just do @t.profit (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.
I tried to put it in application_controller.rb
def index
@t = Total.find(params[:id])
end
and got this error message
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):