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时,我究竟在做些什么?它是控制器吗?
谢谢!
可以粘贴错误和回溯返回一个话题? – Ben 2015-03-30 20:21:05
对不起,刚添加它。还有一点新的,感谢您的耐心! – 2015-03-30 20:22:52
不用担心:) 看起来你刚刚在创建主题(在seeds.rb的第20行左右)后缺少'topics = Topic.all'。 – Ben 2015-03-30 20:24:27