Zach Smith
Zach Smith

Reputation: 8961

Class and module scope in rails helpers

I'm trying to write a rails app that creates an object in the controller based on a helper module, which is written below:

module StockPricesHelper
    require 'net/http'
    class Stock
        attr_accessor(:data)
        def initialize(stock)
            @url = "http://finance.yahoo.com/d/quotes.csv?s=#{stock}&f=sb2b3jk"
        end
        def download_data
            @data = NET::HTTP.get_response(URI.parse(@url)).body
        end
        def clean_string
            @data = @data.strip
        end
        def db_format
            1
        end
    end
end

I get an error uninitialized constant StockPricesHelper::Stock::NET from the rails server.

  1. Am I correctly putting this in a helper module?
  2. What am I doing wrong? I think I'm off on the scope but I don't know where.

Upvotes: 0

Views: 154

Answers (1)

Daniel Rikowski
Daniel Rikowski

Reputation: 72514

You have misspelled the "NET" module. It is Net. (Ruby is case sensitive)

Rails helpers are intended to be view helpers, i.e. aid in generating HTML. It looks like you are performing something which would be better placed in a controller or background job.

Upvotes: 4

Related Questions