As you need to display it in navbar
within layout which I am assuming you need throughout the application, then all you have to do is:
class ApplicationController < ActionController::Base
before_action :set_categories ## Set a before_action
## ...
def set_categories
@categories = Category.all
end
end
This way, since you define a before_action
at ApplicationController
level, set_categories
method will be executed for each and every action throughout your application.