문제

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.

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top