我从Rails 3教程更新了rails 3.1“示例应用程序”。一切顺利,但当我使用该网站并且登录用户试图关注/取消关注其他用户时,我从日志中收到以下消息:
Started POST "/relationships" for 127.0.0.1 at 2011-09-29 20:06:30 -0400
Processing by RelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Yn4XSU4RSEjGqpv1H/ZAxTAi/5JREDaBaa5UbPArRAo=", "relationship"=>{"followed_id"=>"7"}, "commit"=>"Follow"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "7"]]
SQL (3.5ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 30 Sep 2011 00:06:30 UTC +00:00], ["followed_id", 7], ["follower_id", 101], ["updated_at", Fri, 30 Sep 2011 00:06:30 UTC +00:00]]
Relationship Load (0.2ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 101 AND "relationships"."followed_id" = 7 LIMIT 1
Rendered users/_unfollow.html.erb (3.5ms)
(0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = 7
Rendered relationships/create.js.erb (6.7ms)
Completed 200 OK in 51ms (Views: 12.1ms | ActiveRecord: 4.7ms)
Started POST "/relationships" for 127.0.0.1 at 2011-09-29 20:06:31 -0400
Processing by RelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Yn4XSU4RSEjGqpv1H/ZAxTAi/5JREDaBaa5UbPArRAo=", "relationship"=>{"followed_id"=>"7"}, "commit"=>"Follow"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "7"]]
SQL (0.6ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Fri, 30 Sep 2011 00:06:31 UTC +00:00], ["followed_id", 7], ["follower_id", 101], ["updated_at", Fri, 30 Sep 2011 00:06:31 UTC +00:00]]
SQLite3::ConstraintException: constraint failed: INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?)
Completed 500 Internal Server Error in 35ms
SQLite3::ConstraintException (columns follower_id, followed_id are not unique):
操作应该有效,因为这是我第一次创建该关联。在那个错误之后,我需要重新启动服务器才能回到我的用户,否则,服务器会继续投掷。这是一个示例:
Rendered /Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered /Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
Rendered /Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.7ms)
[2011-09-29 20:08:12] ERROR SQLite3::Exception: cannot use a closed statement
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:110:in `close'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:110:in `block in clear_cache!'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:110:in `each'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:110:in `clear_cache!'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb:104:in `disconnect!'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:214:in `block in clear_reloadable_connections!'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `each'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `clear_reloadable_connections!'
/Users/huguesjoly/.rvm/gems/ruby-1.9.2-p180 rails3tutorial/gems/activesupport-3.1.0/lib/active_support/core_ext/module/synchronization.rb:35:in `block in clear_reloadable_connections_with_synchronization!'
...
因此,重启后回到我的用户,我可以看到记录已经创建了。但是,如果我尝试删除该关系,我会从该日志文件中收到以下错误:
Started DELETE "/relationships/95" for 127.0.0.1 at 2011-09-29 20:11:59 -0400
Processing by RelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7YfmrROy4dqviuRakYSyWz2xZLbqIBwfU5McvqBWBrU=", "commit"=>"Unfollow", "id"=>"95"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1
Relationship Load (0.1ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = ? LIMIT 1 [["id", "95"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
Relationship Load (0.1ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 101 AND "relationships"."followed_id" = 7 LIMIT 1
SQL (0.3ms) DELETE FROM "relationships" WHERE "relationships"."id" = ? [["id", 95]]
Rendered users/_follow.html.erb (2.0ms)
(0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = 7
Rendered relationships/destroy.js.erb (5.3ms)
Completed 200 OK in 68ms (Views: 32.5ms | ActiveRecord: 1.5ms)
Started DELETE "/relationships/95" for 127.0.0.1 at 2011-09-29 20:11:59 -0400
Processing by RelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7YfmrROy4dqviuRakYSyWz2xZLbqIBwfU5McvqBWBrU=", "commit"=>"Unfollow", "id"=>"95"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 101 LIMIT 1
Relationship Load (0.1ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = ? LIMIT 1 [["id", "95"]]
Completed 404 Not Found in 28ms
ActiveRecord::RecordNotFound (Couldn't find Relationship with id=95):
app/controllers/relationships_controller.rb:24:in `destroy'
同样,如果我回到用户页面,我可以看到关系已被删除。看起来好像没有理由抛出异常。事实上,如果我对下面的/跟随者关系进行了测试,那么一切都正常。此外,使用rails 3.0的sample_app版本运行良好。
有可能其中一个宝石在rails 3.1的上下文中有错误吗?这是我的Gemfile列表,以防它有用:
source 'http://rubygems.org'
gem 'rails', '3.1.0'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'gravatar_image_tag', '1.0.0'
gem 'will_paginate', '3.0.1'
gem 'sqlite3', '1.3.4'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.1"
gem 'uglifier'
end
# Is replacing: gem 'prototype-rails'
gem 'jquery-rails'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
group :development do
gem 'rspec-rails', '2.6.1'
gem 'annotate', '2.4.0'
gem 'faker', '1.0.0'
end
group :test do
gem 'rspec-rails', '2.6.1'
gem 'webrat', '0.7.3'
gem 'spork', '0.8.5'
gem 'factory_girl_rails', '1.2.0'
# Pretty printed test output
gem 'turn', :require => false
end
提前致谢,
答案 0 :(得分:1)
我遇到了Ajax实现部分的这个问题。问题是因为本教程是为Prototype(pre Rails 3.1)编写的,但Rails 3.1使用的是JQuery。
这是JQuery代码
create.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>");
$("#followers").html('<%= "#{@user.followers.count} followers" %>');
destroy.js.erb
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>"");
$("#followers").html('<%= "#{@user.followers.count} followers" %>');
答案 1 :(得分:0)
这是一个迟到的答案,我在阅读教程时遇到了同样的问题。
检查您的Rsepc测试是否有AbstractController::DoubleRenderError
次失败。如果是这样,您可能忘记在添加redirect_to @user
块后删除RelationbshipsController
中的行respond_to
。