All threads / Can anyone see the error here?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Can anyone see the error here?

Alan Morley asked in Rails

NameError in RecipesController#new

uninitialized constant RecipesController::Category


def new

@recipe = current_user.recipes.build

@categories = Category.all.map{ |c| [c.name, c.id] } <-- saying this lines wrong

end




def create



Hey Alan,

What exactly is the error message?

Also, you should be able to accomplish the same thing using pluck which is more performant, you also don't have to use `all` for this:

@categories = Category.pluck(:name, :id)

Look like it can't find the `Category` model. Did you check to make sure there isn't any misspelling  in the `Category` model name?

uninitialized constant RecipesController::Category

Sorry, I didn't see that line before

As Toan said, check the spelling and file names for your category model. You should have a file in the models directory called category.rb (all lowercase) with class Category < ApplicationRecord (rails 5+) or class Category < ActiveRecord::Base if <= Rails 4

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,399+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.