4

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!

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ok. Can I use @buy and @sell normally or do I have to do something like this: @buy.set_prices?
@user3024194 You can just use @buy and @sell.
2

Two ways:

  1. 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

  2. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.