Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:43:21+00:00 2026-05-30T17:43:21+00:00

I have Categories controller and in layout _menu.html.erb I want to output all categories

  • 0

I have Categories controller and in layout _menu.html.erb I want to output all categories in home page but I have this error message:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

When I logged as a administrator I can add, edit, delete and view all categories.

This is part of my code:

_menu.html.erb

<div class="menu">
  <% @categories.each do |category| %>
    <li>
      <%= link_to category.title, category %>
    </li>
  <% end %>
</div>

Categories_controller.rb

  def index
    @title = "All categories"
    @categories = Category.paginate(:page => params[:page])
  end

  def show
    @category = Category.find(params[:id])
    @title = @category.title
  end

  def new
    @category = Category.new
    @title = "Add category"
  end

  def create
    @category = Category.new(params[:category])
   if @category.save
     flash[:success] = "Successfully added category"
     redirect_to categories_path
   else
     @title = "Sign up"
     render 'new'
   end
 end

 def edit
   @category = Category.find(params[:id])
   @title = "Edit category"
 end

 def update
   @category = Category.find(params[:id])
   if @category.update_attributes(params[:category])
     flash[:success] = "Category updated."
     redirect_to categories_path
   else
     @title = "Edit user"
     render 'edit'
   end
 end

 def destroy
   Category.find(params[:id]).destroy
   flash[:success] = "User destroyed."
   redirect_to categories_path
 end

end

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:43:22+00:00Added an answer on May 30, 2026 at 5:43 pm

    @categories are defined only in the index action. I’m assuming you are using the _menu.html.erb as a partial in the layout – on every page.

    The @categories will be nil for the others which will cause the exception.

    There are basically two ways to define categories for all actions.
    One would be to do the call in the partial like

    <% Category.all.each do |category| %>

    The other way using a before filter in your controller

    class CategoriesController
      before_filter :load_categories
    
      ...
    
      private
    
      def load_categories
        @categories = Category.all
      end
    end
    

    I personally prefer the second way because I don’t like database calls triggered in views.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application.html.erb (main layout) has some categories on the top of the header, and
i have some controllers, i want to put categories names into layout, like menu,
I have a categories table view controller. In my app, categories can have subcategories.
I have main categories and sub categories. I want to be able to sort
I need get all items these have no categories int? categoryId = null; var
I am pulling categories from an xml file. I only have 5 categories but
I have a controller called Categories and you are allowed to type a few
Okay, so I have a UITableView hierarchy. The top level controller is for Categories,
I have quite a large View Controller in my app and I want to
So I have had various forms of this working in the last while, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.