0

I keep getting the following error in my terminal when I rake db:reset. I am not sure why the topics object is undefined. I have created a topics controller and model, here the code:

NameError: undefined local variable or method `topics' for main:Object
/Users/ericpark/rails_projects/code/bloccit/db/seeds.rb:26:in `block in <top (required)>'
/Users/ericpark/rails_projects/code/bloccit/db/seeds.rb:23:in `times'
/Users/ericpark/rails_projects/code/bloccit/db/seeds.rb:23:in `<top (required)>'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/railties-4.2.0/lib/rails/engine.rb:547:in `load_seed'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Users/ericpark/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:setup => db:seed
Error

Topics Controller

class TopicsController < ApplicationController
  def index
    @topics = Topic.all
    #authorize @topics
  end

  def new
    @topic = Topic.new
    authorize @topic
  end

  def show
    @topic = Topic.find(params[:topic_id])
    #authorize @topic
  end

  def edit
    @topic = Topic.find(params[:id])
    authorize @topic
  end

  def create
     @topic = Topic.new(params.require(:topic).permit(:name, :description, :public))
     authorize @topic
     if @topic.save
       redirect_to @topic, notice: "Topic was saved successfully."
     else
       flash[:error] = "Error creating topic. Please try again."
       render :new
     end
   end

   def update
     @topic = Topic.find(params[:id])
     authorize @topic
     if @topic.update_attributes(params.require(:topic).permit(:name, :description, :public))
       redirect_to @topic
     else
       flash[:error] = "Error saving topic. Please try again."
       render :edit
     end
   end
end

Topics Model

class Topic < ActiveRecord::Base
  has_many :posts
end

Seed.rb

require 'faker'

5.times do
   user = User.new(
     name:     Faker::Name.name,
     email:    Faker::Internet.email,
     password: Faker::Lorem.characters(10)
   )
   user.skip_confirmation!
   user.save!
 end
 users = User.all

 #Create Topics
 15.times do
   Topic.create!(
     name:         Faker::Lorem.sentence,
     description:  Faker::Lorem.paragraph
   )
 end

 # Create Posts
 50.times do
   Post.create!(
    user:   users.sample,
      topic:  topics.sample,
    title:  Faker::Lorem.sentence,
    body:   Faker::Lorem.paragraph
   )
 end
 posts = Post.all

 # Create Comments
 100.times do
   Comment.create!(
      #We have not yet associated users with comments
     post: posts.sample,
     body: Faker::Lorem.paragraph
   )
 end

topics=Topic.all

 # user = User.first
 # user.skip_reconfirmation!
 # user.update_attributes!(
 #   email: 'erichoonpark@gmail.com',
 #   password: 'bullseye'
 # )

 admin = User.new(
  name: 'Admin User',
  email: 'admin@example.com',
  password: 'examplepass',
  role: 'admin'
 )
 admin.skip_confirmation!
 admin.save!

 moderator = User.new(
  name: 'Moderator',
  email: 'moderator@example.com',
  password: 'examplepass',
  role: 'moderator'
 )
 moderator.skip_confirmation!
 moderator.save!

 member = User.new(
  name: 'User',
  email: 'user@example.com',
  password: 'examplepass'
 )
 member.skip_confirmation!
 member.save!


 puts "Seed finished"
 puts "#{User.count} users created"
 puts "#{Post.count} posts created"
 puts "#{Comment.count} comments created"

Could someone explain why the topics is undefined for the terminal? What exactly am I pulling when I create a topics.sample? Is it the controller?

Thanks!

Eric Park
  • 502
  • 2
  • 7
  • 23

1 Answers1

0

It's nothing to do with the controller. topics isn't defined in seeds.rb because nothing has defined it. You probably meant to set

topics = Topic.all

After you have created the topics and then topics.sample would return a topic at random from that array

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • Thank you so much Frederick! I have been working on this for two hours now. Being a noobie, I still have some trouble reading the errors. – Eric Park Mar 30 '15 at 20:28