I am trying to build a little web application with Ruby on Rails. It's a simple tool to track stocks that people are interested in.
For the stock data I am using the stock_quote gem.
Currently, I have a model with a single field, 'ticker'. Here people would enter the ticker symbol of the stock they're interested in watching. I want to be able to populate a table of information based on the ticker when the user logs in (full company name, price etc). When I test my current method, I get a " uninitialized constant Target::StockQuote" error. However, when I call my methods get_current_price or get_company_name in the console, they work perfectly. I know I need to keep my views dumb, but I'm not sure how to make it more simple than this- and have them work properly.
I would like to be able to populate my dashboard table with information on the stocks users are watching. Given that the model stores the ticker (i.e. AAPL), I need to somehow input the ticker into a series of methods in the model that pull the proper data from the stock_quote API and return the result to my HTML table.
Thanks for the help
stock.rb
class Stock < ActiveRecord::Base
attr_accessible :ticker
def get_company_name(ticker)
company_name = StockQuote::Stock.quote(ticker).company
end
def get_current_price(ticker)
company_name = StockQuote::Stock.quote(ticker).last
end
end
stocks_controller.rb
class StocksController < ApplicationController
def dashboard
@stocks = Stock.all
end
end
dashboard.html.erb
<tbody>
<% @stocks.each do |stock| %>
<tr>
<td><%= stock.get_company_name(stock.ticker) %></td>
<td><%= stock.stock %></td>
<td><%= button_to "Delete", edit_target_path, :class => "btn btn-small btn-danger" %> </td>
</tr>
<% end %>
</tbody>