I have a navbar that is placed in the /views/layouts/application.html.erb and in there I place the @buy and @sell variables which get the latest bitcoin price. They are currently placed in the /welcome controller so the prices only show when in the welcome controller. If I navigate to another controller, the bar just becomes empty.
@buy = coinbase.buy_price
@sell = coinbase.sell_price
That is the code that is currently placed in the welcome controller. I want it to be available to the navbar regardless of what controller the user is in. Any help would be great!
place it in the application controller and use a before action
before_action :set_prices
def set_prices
@buy = coinbase.buy_price
@sell = coinbase.sell_price
end
Two ways:
Write the code in application_controller as before_filter, and use the variables in nav bar.
before_filter :prices
def prices
@buy = coinbase.buy_price
@sell = coinbase.sell_price
end
Write the code in application.html.erb, just before the nav bar code:
<% @buy = coinbase.buy_price %>
<% @sell = coinbase.sell_price %>
And use them directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With