当我使用db:reset时,我的终端中出现以下错误。我不确定为什么主题对象是未定义的。我创建了一个主题控制器和模型,这里是代码:
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: '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"
有人可以解释为什么终端的主题未定义?在创建topic.sample时,我究竟在拉什么?是控制器吗?
谢谢!
答案 0 :(得分:0)
它与控制器无关。 topics
未在seeds.rb中定义,因为没有定义它。你可能想要设置
topics = Topic.all
创建主题后,topics.sample
将从该数组中随机返回一个主题