Ruby on Rails 3 教程的错误,第 13 章

发布于 2024-12-07 06:41:20 字数 7762 浏览 6 评论 0原文

我将 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版本运行得很好。

有没有可能其中一个 gem 在 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

提前致谢,

I updated to rails 3.1 the "sample application" from the Rails 3 Tutorial. Everything went smoothly except that when I'm using the site and a logged in user tries to follow/unfollow another user, I get the following message from the log:

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):

The operation should work since it's the first time that I'm creating that association. After that error, I need to do a restart of the server in order to get back to my user, otherwise, the server keeps on throwing. Here's a sample:

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!'
    ...

So, back to my user after a restart, I can see that the record has been created anyway. But if I try to delete that relation, I get the following error from that log file:

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'

Again, if I get back to the user's page, I can see that the relation has been removed. It looks as if exceptions are thrown for no reason. And indeed, if I rspec-test the following/follower relations, everything works correctly. Furthermore, the version of the sample_app using rails 3.0 works perfectly.

Could it be possible that one of the gems has a bug in the context of rails 3.1? Here's a listing of my Gemfile in case that it would help:

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

Thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-12-14 06:41:20

我在 Ajax 实现部分遇到了这个问题。问题是因为本教程是为 Prototype(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" %>'); 

I had this problem for the Ajax implementation part. The problem was because the tutorial is written for Prototype (pre Rails 3.1), but Rails 3.1 uses JQuery.

This is the JQuery code

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" %>'); 

source

聚集的泪 2024-12-14 06:41:20

这是一个迟到的答案,我在阅读教程时遇到了同样的问题。

检查您的 Rsepc 测试是否有 AbstractController::DoubleRenderError 失败。如果是这样,您可能忘记在添加 respond_to 块后删除 RelationbshipsController 中的 redirect_to @user 行。

This is a late answer, I ran into the same problem when going through the tutorial.

Check if your Rsepc tests have AbstractController::DoubleRenderError failures. if so you probably forget to remove the line redirect_to @user in the RelationbshipsController after adding the respond_to block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文