Michael Porter
Michael Porter

Reputation: 229

Ruby on rails No method error.

I'm running into the infamous No Method Error. I've worked my way through a number of examples here on STOF but I can't see an error in my code that stands out. I've checked that rake routes matches what I think should be happening and the paths provided from using resources in the routes.db file seem to be correct. I know I'm missing some small detail but I can't for the life of me see it now. Any help would be appreciated.

Error Screenshot

My Controller code:

class GenevarecordsController < ApplicationController

  def index
    @genevarecords = GenevaRecord.all.page(params[:page]).per(5)
  end

  def new
    @genevarecord = GenevaRecord.new
  end

end

My routes:

Rails.application.routes.draw do

  root 'genevarecords#index'

  resources :genevarecords

end

Upvotes: 0

Views: 422

Answers (2)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

You need to take Did you mean? section seriously,

Anyway, if you closely look at ruby syntax following is the representation for the class name,

AbcDef and equivalent snake case is abc_def

In your case,

Your model is named as GenevaRecord but your controller is GenevarecordsController

change it to GenevaRecordsController, also you need to match it's equivalent snake case in routes...

Rails.application.routes.draw do

  root 'geneva_records#index'

  resources :geneva_records

end

So, when you pass @genevarecord to the form it is initialized as GenevaRecord.new and searches for geneva_records_path which is undefined because you have defined it as genevarecords_path which doesn't match you model (resources)..

Hope it helps in understanding..

Upvotes: 0

John Hayes-Reed
John Hayes-Reed

Reputation: 1428

You have a naming discrepency between your model and your controller / routes. your model is GenevaRecord, underscored makes it geneva_record. However your controller only has a single capital letter at the beginning: Geneverecords which underscored would be genevarecords. Therefore when you pass your model to the form it tries to use a controller / routes helpers with the same naming format as the model, which would be geneva_records_controller ie. GenevaRecordsController.

What you need to do is match your controller and routes to the same naming format as your model:

class GenevaRecordsController < ApplicationController
  #...
end

Rails.application.routes.draw do
  #...
  resources :geneva_records
end

Upvotes: 2

Related Questions