这似乎应该是一个简单的问题,但我被卡住了。
我有一个Book模型,想要通过一个包含isbns列表的csv创建一堆书。我创建了一个rake任务 -
task :import_books => :environment do
CSV.foreach('lib/books.csv', :headers => true, :encoding => 'ISO-8859-1:UTF-8') do |row|
Book.create!({:isbn => row[0]})
end
end
但是我收到以下错误 -
ActionController :: UrlGenerationError:没有路由匹配{:action =>" show",:controller =>" books",:id => nil}缺少必需的密钥:[:id]
fwiw,从控制台,我可以使用Model.create在所有其他模型中创建记录,但不能创建Book。
我尝试使用id进行硬编码,但它没有工作,看起来并不理想。
感谢您的帮助!
编辑 -
图书迁移(您的意思是架构吗?) -
create_table "books", force: true do |t|
t.string "title"
t.string "author"
t.text "description"
t.string "link"
t.integer "score"
t.boolean "featured"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.boolean "likes"
t.string "cover_file_name"
t.string "cover_content_type"
t.integer "cover_file_size"
t.datetime "cover_updated_at"
t.string "image_remote_url"
t.string "cover_url"
t.string "fake_cover"
t.boolean "permission"
t.date "release"
t.string "isbn"
end
Book.new和Book.create from console -
2.1.2:005> Book.new
=> #
2.1.2:006> Book.create
(10.2ms)开始交易
SQL(3.4ms)INSERT INTO" books" (" created_at"," updated_at")VALUES(?,?)[[" created_at"," 2015-01-26 17:50:11.119126 "],[" updated_at"," 2015-01-26 17:50:11.119126"]]
(1.1ms)回滚事务
ActionController :: UrlGenerationError:没有路由匹配{:action =>" show",:controller =>" books",:id => nil}缺少必需的键:[: ID]
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.7/lib/action_dispatch/routing/route_set.rb:219:in raise_generation_error'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.7/lib/action_dispatch/routing/route_set.rb:192:in
optimized_helper'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.7/lib/action_dispatch/routing/route_set.rb:178:in call'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/actionpack-4.1.7/lib/action_dispatch/routing/route_set.rb:270:in
块(2级)in define_url_helper& #39;
来自/Users/danifank/Documents/coverlist/app/models/book.rb:124:in load_into_soulmate'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:424:in
块中的make_lambda'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:221:in call'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:221:in
阻止halting_and_conditional'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:215:in call'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:215:in
阻止halting_and_conditional'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:86:in call'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:86:in
run_callbacks'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in create_or_update'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/persistence.rb:103:in
save'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/validations.rb:51:in save'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:21:in
save'
...... 3级......
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in within_new_transaction'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in
transaction'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/transactions.rb:208:in transaction'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/transactions.rb:326:in
with_transaction_returning_status'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in block in save'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/transactions.rb:283:in
rollback_active_record_state!'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/transactions.rb:267:in save'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.7/lib/active_record/persistence.rb:34:in
创建'
来自(irb):6
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/railties-4.1.7/lib/rails/commands/console.rb:90:in start'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/railties-4.1.7/lib/rails/commands/console.rb:9:in
start'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:69:in console'
from /Users/danifank/.rvm/gems/ruby-2.1.2/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:40:in
run_command!'
来自/Users/danifank/.rvm/gems/ruby-2.1.2/gems/railties-4.1.7/lib/rails/commands.rb:17:in <top (required)>'
from bin/rails:4:in
要求&#39;
来自bin / rails:4:在`&#39; 2.1.2:007&gt;