Frage

In my navbar, I have a variable perhaps @categories = Category.all. There is a drop down in the nav that lists the categories.

I'm wondering where I put this because it is a variable for the layout and not for a page. I thought about putting it in a controller, but that doesn't work.

Does this go the application helper?

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top