NoMethodError

Hello fellow Ruby enthusiasts!

Although I’ve been using Ruby for some time, I just started using
RoR. I’m experiencing an error that I’m having trouble resolving and
was hoping to find some help.

I am getting this error message when I try to hit a certain URL:

http://localhost:3000/captionfiles

NoMethodError in CaptionfilesController#index
undefined method all' for Captionfile:Class app/controllers/captionfiles_controller.rb:5:inindex’

Here are my files:

class CaptionfilesController < ApplicationController
def index
@captionfiles = Captionfile.all

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @captionfiles }
end

end

class Captionfile < ActiveRecord::Base
validates :title, :presence => true, :length => { :minimum => 2 }
validates :filename, :presence => true
has_many :run
end

Cccweb::Application.routes.draw do
resources :captionfiles
resources :runs
get “pages/choose”
get “home/index”
get “runs/new”
root :to => ‘home#index’
end

I’ve searched the internets as well as many message bases, and
although I come up with variants of this message, none of the
solutions seem to help.

Thank you!

On 9 March 2012 19:52, AndyLikesRuby [email protected] wrote:

NoMethodError in CaptionfilesController#index
format.html # index.html.erb
format.json { render json: @captionfiles }
end
end

class Captionfile < ActiveRecord::Base
validates :title, :presence => true, :length => { :minimum => 2 }
validates :filename, :presence => true
has_many :run
end

Can you do anything useful with the Captionfile class? Creating
records for example.

I assume you have you tried restarting the server.

Are you sure you have not got Captionfile declared in another file
somewhere? Perhaps you kept a copy of captionfiles.rb with an out of
date definition in it. Do a global source search for Captionfile in
your project.

That should be has_many :runs by the way (plural) but I don’t think
that is anything to do with this problem.

Colin

On 9 March 2012 19:52, AndyLikesRuby [email protected] wrote:

class Captionfile < ActiveRecord::Base
There’s nothing, at first glance, wrong with your code. So can you
just confirm which version of Rails you have installed. Older (very
old!) versions didn’t have “.all”, you had to use “.find(:all)”. But
if that’s your problem, you must be on Rails 1.x, and that won’t be
the last of your problems…

Hi Michael – I am using Ruby v1.9.2p290, Rails v3.2.2, and rvm 1.8.5.

I checked to see if I had duplicate files, but to no avail. I will do
some deep digging this morning. If I find a solution, I’ll post it.

Thanks for your help.

Andy

Hi Colin,

I did try restarting the server, etc. I still can’t fix the problem.

I am going to do some deep-diving into the code today. I suspect it
has something to do with a file like routes.db, or similar. The code
seems to be ok – there’s something else that isn’t quite evident
that’s causing this issue.

Thanks for your help!

Andy

Hi Colin,

Thanks for the reply. Your question prompted me to search around my
class declarations, and sure enough, I found the problem.

I declared two container classes, Captionfile and Run, to capture
information in a convenient, encapsulated format. From there I create
objects to make arrays/hashes for processing

I stored them in the /lib directory, which apparently conflicted with
the ActiveRecord class declarations of the same name.

I renamed the class files and suddenly it all worked. I now have to
figure out how to include these class files without conflict (I’ll
likely rename them).

Thanks for your tipoff! That’s what lead me to my solution.

Cheers,
Andy

On 13 March 2012 17:36, AndyLikesRuby [email protected] wrote:

Hi Colin,

I did try restarting the server, etc. I still can’t fix the problem.

You did not say whether you can do anything useful with the class at
all (probably because you top posted rather than inserting comments
inline). This would have told us whether it is something specific to
do with #all or whether your class is not being recognised as an
ActiveRecord class.

Colin

On Mar 11, 2:49pm, Colin L. [email protected] wrote:

Hello fellow Ruby enthusiasts!
undefined method `all’ for Captionfile:Class
format.json { render json: @captionfiles }
records for example.

Colin


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

On 13 March 2012 21:55, AndyLikesRuby [email protected] wrote:

the ActiveRecord class declarations of the same name.
That is why I suggested doing a global search for Captionfile.

I renamed the class files and suddenly it all worked. I now have to
figure out how to include these class files without conflict (I’ll
likely rename them).

You can’t have two different classes of the same name.

Thanks for your tipoff! That’s what lead me to my solution.

Glad to be of help

Colin

Does it work when you run Captionfile.all in your rails console?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs