0

Here is my need: I have a table categories of a forum application. I need a variable that I can use in my app/views/layouts/application.html.erb so I can create a dropdown in the navbar with all categories in all the pages.

There's just one way I saw this as possible but as I am new to Rails I don't think it is the best, that is creating a $categories = Category.all. But as I said, it looks dangerous.

What would the best way to solve my problem?

1 Answer 1

2

You need to get data into app/views/layouts/application.html.erb?

There's a controller just for that!

It's called ApplicationController.

In your ApplicationController do the following:

class ApplicationController < ActionController::Base
  before_action :set_categories

  # stuff..

  private

  def set_categories
    @categories = Category.all
  end
end

Now you have access to a @categories instance variable throughout your application code, including application layout.

Sign up to request clarification or add additional context in comments.

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.