0

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

Full stacktrace

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>
tereško
  • 58,060
  • 25
  • 98
  • 150
Questifer
  • 1,113
  • 3
  • 18
  • 48
  • 1
    Please post a full stacktrace of your exception and information about what you are trying to do – usha Jun 06 '13 at 20:50
  • Where does `edit_target_path` come from. Can we see the class for this and the routes file because you say you are getting `uninitialized constant Target::StockQuote` error but there is no `Target` class. – ChuckJHardy Jun 07 '13 at 11:56

1 Answers1

-1

You need to update your button to point at the stock object and tell it the action & method for that action are delete.

<td><%= button_to "Delete", {:action => "delete", :id => stock.id }, :method => :delete, :class => "btn btn-small btn-danger" %></td>

Please see: http://apidock.com/rails/v3.2.8/ActionView/Helpers/UrlHelper/button_to

Or, I think this syntax is nicer:

<td><%= link_to "Delete", stock, :method => :delete, :class => "btn btn-small btn-danger" %></td>

Bonus

You've got an inefficiently written Stock class. Since you're calling instance methods, you don't need to pass the ticker value to get_company_name and get_current_price. The instance methods are operating within the context of a specific Stock object already, so they already know what the value for ticker is. That's what it means to call the method on stock in your view, ie stock.company_name.

class Stock < ActiveRecord::Base
  attr_accessible :ticker

  def company_name
    StockQuote::Stock.quote(ticker).company
  end

  def current_price
    StockQuote::Stock.quote(ticker).last
  end
end
Carlos Drew
  • 1,633
  • 9
  • 17