Question

I'm using a gem that makes an API call and storing it in a variable within my Rails 4 app. I only need access to the variable in my Show view. It seems to be working fine, but I'm not sure if this is the correct way to do this.

Here's what I have in my controller:

before_action :call_yahoo, only: [:show]

def show
end

private

def call_yahoo
  @stockdata = StockQuote::Stock.quote(@stock.ticker)
end

Is this the best place for a call like this? I'm new to Rails and want to make sure that this variable isn't being loaded every time a view besides Show is accessed.

Était-ce utile?

La solution

if it is only in the @show method, do you even need the before_action.

def show
 @stockdata ||= StockQuote::Stock.quote(@stock.ticker)
end

just call it in the show action?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top