我是一名新的铁杆学生,负责一个有用户登录的项目,每个用户都有一个列表,每个列表都有很多项目。项目嵌套在我的路线中的列表下。
我遇到了一些问题。我的代码下面为该用户加载了一个用户和一个列表,但无法为列表加载项目。我的想法是首先让它工作,但最后我想拥有5个用户,每个用户都有一个列表,每个列表上有15个项目都是由faker创建的。任何帮助表示赞赏。
require 'faker'
# Create an admin user
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld'
)
admin.skip_confirmation!
admin.save!
# Create List
1.times do
List.create(
user: admin,
title: 'Admin List'
)
end
# Create Items
15.times do
Item.create(
list: 'Admin List',
name: Faker::Company.bs
)
end
puts "Seed finished"
puts "#{User.count} users created"
puts "#{List.count} lists created"
puts "#{Item.count} items created"
这是完整的错误
heidi-golds-imac:blocitoff dale$ rake db:seed
rake aborted!
ActiveRecord::AssociationTypeMismatch: List(#70281090559180) expected, got String(#70281054267240)
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/association.rb:216:in `raise_on_type_mismatch!'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:12:in `replace'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/associations/builder/association.rb:123:in `list='
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `public_send'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `each'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in `init_attributes'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in `initialize'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/persistence.rb:33:in `create'
/Users/dale/code/blocitoff/db/seeds.rb:22:in `block in <top (required)>'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `times'
/Users/dale/code/blocitoff/db/seeds.rb:21:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.0/gems/railties- 4.2.0/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.0/gems/activerecord- 4.2.0/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/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)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
项目控制器
class ItemsController < ApplicationController
before_action :find_list
def create
@item = @list.items.build(params.require(:item).permit(:name))
unless @item.save
flash[:error] = "There was an error. Please try again."
end
redirect_to @list
end
private
def find_list
@list = List.find(params[:list_id])
end
end
列出控制器
class ListsController < ApplicationController
before_action :authenticate_user! # users must be signed in before any lists_controller method
def show
@list = current_user.list
end
def new
@list = List.new
end
def create
@list = current_user.build_list(params.require(:list).permit(:title, :body))
if @list.save
flash[:notice] = "List was saved."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :new
end
end
def edit
@list = List.find(params[:id])
end
def update
@list = List.find(params[:id])
if @list.update_attributes(params.require(:list).permit(:title, :body))
flash[:notice] = "List was updated."
redirect_to @list
else
flash[:error] = "There was an error. Please try again."
render :edit
end
end
end
答案 0 :(得分:2)
尝试更正以下代码:
# Create Items
15.times do
Item.create(
list: 'Admin List'
name: Faker::Company.bs
)
end
只需在列表后添加逗号:&#39;管理员列表&#39; 如果这不能解决问题,请发布完整的错误消息并指定您收到错误的行。
答案 1 :(得分:1)
问题是因为您是按名称而不是通过引用将列表分配给项目。在您创建列表的代码块中,您需要将其分配给变量:
list = List.create ...,
然后,在创建项目时,您需要执行以下操作:
Item.create(list:list ....
您正在使用列表的标题,它正在寻找列表对象。