2015-03-30 32 views
0

當我耙db:重置時,我不斷在我的終端中出現以下錯誤。我不知道爲什麼主題對象是未定義的。我創建了一個主題控制器和模型,這裏的代碼:名稱錯誤:主題,耙db:重置錯誤,我無法確定的來源

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 

主題控制器

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 

主題模式

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: '[email protected]', 
# password: 'bullseye' 
#) 

admin = User.new(
    name: 'Admin User', 
    email: '[email protected]', 
    password: 'examplepass', 
    role: 'admin' 
) 
admin.skip_confirmation! 
admin.save! 

moderator = User.new(
    name: 'Moderator', 
    email: '[email protected]', 
    password: 'examplepass', 
    role: 'moderator' 
) 
moderator.skip_confirmation! 
moderator.save! 

member = User.new(
    name: 'User', 
    email: '[email protected]', 
    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" 

可能有人解釋爲什麼話題是不確定的編輯爲終端?當我創建topics.sample時,我究竟在做些什麼?它是控制器嗎?

謝謝!

+0

可以粘貼錯誤和回溯返回一個話題? – Ben 2015-03-30 20:21:05

+0

對不起,剛添加它。還有一點新的,感謝您的耐心! – 2015-03-30 20:22:52

+1

不用擔心:) 看起來你剛剛在創建主題(在seeds.rb的第20行左右)後缺少'topics = Topic.all'。 – Ben 2015-03-30 20:24:27

回答

0

這與控制器無關。 topics未在seeds.rb中定義,因爲沒有定義它。你可能只是設定

topics = Topic.all 

您所創建的主題後,然後將topics.sample隨機從數組

+0

非常感謝弗雷德裏克!我一直在爲此工作了兩個小時。作爲一個noobie,我仍然有一些麻煩閱讀錯誤。 – 2015-03-30 20:28:19