Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top