4

Bug with Ruby on Rails 3 tutorial, chapter 13

 2 years ago
source link: https://www.codesd.com/item/bug-with-ruby-on-rails-3-tutorial-chapter-13.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Bug with Ruby on Rails 3 tutorial, chapter 13

advertisements

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,


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK