1

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'

繰り返しますが、ユーザーのページに戻ると、関係が削除されていることがわかります。理由もなく例外がスローされたかのように見えます。実際、following/follower 関係を rspec-test すると、すべてが正しく機能します。さらに、rails 3.0 を使用する sample_app のバージョンは完全に動作します。

Rails 3.1 のコンテキストで gem の 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

前もって感謝します、

4

2 に答える 2

1

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

ソース

于 2012-04-07T20:35:05.003 に答える
0

これは遅い答えです。チュートリアルを進めているときに同じ問題に遭遇しました。

Rsepc テストにAbstractController::DoubleRenderError失敗がないかどうかを確認します。もしそうなら、ブロックを追加redirect_to @userした後に行を削除するのを忘れている可能性があります。RelationbshipsControllerrespond_to

于 2014-01-07T02:52:46.710 に答える