Ruby on Rails Tuesday, May 29, 2012



On 29 May 2012 12:56, Steve Knit <lists@ruby-forum.com> wrote:
I am still learning Rails, I've read some books and did some hands on at
codeschool.com and now I'm trying to write my first simple app from
scratch.

I'm using devise for authentication, but since i still kinda suck at
rails, I haven't gotten email confirmation working so currently, for
testing purposes only Admin users can take actions.

Here are my models:


   [loluser@fedora models]$ ls
   admin.rb  pet.rb  user.rb
   [loluser@fedora models]$ cat admin.rb
   class Admin < ActiveRecord::Base
     has_many  :pets
     devise :database_authenticatable, :registerable, :timeoutable,
:validatable,
            :timeout_in => 20.minutes
   end
   [loluser@fedora models]$ cat pet.rb
   class pet < ActiveRecord::Base
     belongs_to :admin
   end
[loluser@fedora models]$


In my controller, I want to display Admin[1]'s pets in the index  so i
have this code:

     class petsController < ApplicationController
     before_filter :authenticate_admin!
     # GET /pets
     # GET /pets.xml
     def index
     admin=Admin.find(1)
       @pets = pet.admin.all

I'm guessing your pet object doesn't have an admin method?

Should "pet.admin.all" maybe be "admin.pets" ?

 

       respond_to do |format|
         format.html # index.html.erb
         format.xml  { render :xml => @pets }
       end
     end

However, I am getting this error:

   NoMethodError in petsController#index

   undefined method `admin' for #<Class:0x7f2daa7b0258>

   Rails.root: /home/loluser/dev/app2/devise_example
   Application Trace | Framework Trace | Full Trace

   app/controllers/pets_controller.rb:7:in `index'

Help will be appreciated and please let me know if I need to clarify
something.

Thanks in advance.

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment