0

I've got a ruby on rails application and am trying to save category data to the system, but whenever I click save I get the error:

NoMethodError in CategoriesController#create
undefined method `category' for #<Category id: nil, genre: "", created_at: nil, updated_at: nil>
Extracted source (around line #29):

27 def create
28 @category = Category.new(category_params)
29 if @category.save
30 redirect_to @category, notice: 'Category was successfully created.'
31 else
32 render action: 'new'

Rails.root: C:/Sites/week_15/New/my_bookshop_test2 _basic

This is my code in the categories_controller.rb:

class CategoriesController < ApplicationController
   before_action :set_category, only: [:show, :edit, :update, :destroy]
   def new
     @category = Category.new
   end
   def create
     @category = Category.new(category_params)
     if @category.save
       redirect_to @category, notice: 'Category was successfully created.'
     else
       render action: 'new'
     end
   end

Can someone please help me.

Anthony
  • 15,435
  • 4
  • 39
  • 69
  • show the full error stack – Arup Rakshit Jan 08 '15 at 16:00
  • Post a full stacktrace, just like in this question: http://stackoverflow.com/questions/4980877/rails-error-couldnt-parse-yaml – Magnuss Jan 08 '15 at 16:39
  • We need your category_params – Anthony Jan 08 '15 at 16:52
  • # Never trust parameters from the scary internet, only allow the white list through. def category_params params.require(:category).permit(:genre) end –  Jan 08 '15 at 17:41
  • can you also post code from your category model? – mswiszcz Jan 08 '15 at 17:45
  • class Category < ActiveRecord::Base has_many :products validates :category, :presence => { :message => "cannot be blank ..."} # display id and genre text e.g. 1 Detective, 2 Science Fiction, etc. # used in category drop down selection box def category_info "#{id} #{genre}" end end –  Jan 08 '15 at 17:55
  • would be better to edit your question and add code formatted, but for me it seems that you are validating for presence of category in category itself. Should category belong to/have many other category / categories? – mswiszcz Jan 08 '15 at 18:07
  • One category has many products, if that helps –  Jan 08 '15 at 19:02

1 Answers1

0

You should update the code from the comments in your question, because your model has a clear mistake in it:

Your code:

class Category < ActiveRecord::Base 
  has_many :products
  validates :category, :presence => { :message => "cannot be blank ..."} 
  # display id and genre text e.g. 1 Detective, 2 Science Fiction, etc. 
  # used in category drop down selection box    
  def category_info 
    "#{id} #{genre}"
  end 
end 

If you take a look at the validation, you are trying to validate category itself instead of whatever it is you need to validate(from your previously posted code it looks like you need to validate genre)

For your code to work, you need to change it to this:

class Category < ActiveRecord::Base 
  has_many :products
  validates :genre, :presence => { :message => "cannot be blank ..."} 
  # display id and genre text e.g. 1 Detective, 2 Science Fiction, etc. 
  # used in category drop down selection box    
  def category_info 
    "#{id} #{genre}"
  end 
end

You use validations on attributes( like genre in your case), not the object itself.

Kkulikovskis
  • 2,028
  • 16
  • 28