在迈克尔·哈特尔的Ruby on Rails Tutorial第12章中,用户的个人资料页面上有一个按钮,您可以翻转跟随或取消关注他/她。 (见图12.12和12.13)
我正在使用rvm 1.8.6和Rails 3.1.1。
当我使用Prototype时,单击按钮的行为可以正常工作。 在/sample_app/app/views/layouts/application.html.erb,
<%= javascript_include_tag :defaults %>
点击“关注”时,首先执行POST,然后GET重新呈现页面。日志如下。
Started POST "/relationships" for 127.0.0.1 at 2011-11-30 22:07:04 -0800
Processing by RelationshipsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sVoNM/yyDmn0czvbza+dxzLsPW4mPNlGxAI1vKN7Ez4=", "relationship"=>{"followed_id"=>"13"}, "commit"=>"Follow"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "13"]]
SQL (0.6ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 01 Dec 2011 06:07:04 UTC +00:00], ["followed_id", 13], ["follower_id", 1], ["updated_at", Thu, 01 Dec 2011 06:07:04 UTC +00:00]]
Redirected to ht.p://localhost:3000/users/13
Completed 302 Found in 52ms
Started GET "/users/13" for 127.0.0.1 at 2011-11-30 22:07:04 -0800
Processing by UsersController#show as HTML
Parameters: {"id"=>"13"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "13"]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Relationship Load (0.2ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 1 AND "relationships"."followed_id" = 13 LIMIT 1
CACHE (0.0ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 1 AND "relationships"."followed_id" = 13 LIMIT 1
Rendered users/_unfollow.html.erb (6.9ms)
Rendered users/_follow_form.html.erb (54.5ms)
(0.2ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 13
CACHE (0.0ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 13
(0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = 13
(0.1ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = 13
Rendered shared/_stats.html.erb (4.2ms)
Rendered users/show.html.erb within layouts/application (67.5ms)
Rendered layouts/_stylesheets.html.erb (1.4ms)
Rendered layouts/_header.html.erb (2.5ms)
Rendered layouts/_footer.html.erb (0.7ms)
Completed 200 OK in 100ms (Views: 83.1ms | ActiveRecord: 1.9ms)
但是,在我按照第13章并切换到JQuery之后,单击跟随按钮的行为导致异常并导致Web服务器崩溃。 我将/sample_app/app/views/layouts/application.html.erb修改为
后<%= javascript_include_tag "application" %>
它会做两次POST。我猜数据库唯一的违规行为破坏了Ruby。日志如下。
Started POST "/relationships" for 127.0.0.1 at 2011-11-30 21:08:51 -0800
Processing by RelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sVoNM/yyDmn0czvbza+dxzLsPW4mPNlGxAI1vKN7Ez4=", "relationship"=>{"followed_id"=>"19"}, "commit"=>"Follow"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "19"]]
SQL (4.6ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 01 Dec 2011 05:08:51 UTC +00:00], ["followed_id", 19], ["follower_id", 1], ["updated_at", Thu, 01 Dec 2011 05:08:51 UTC +00:00]]
Relationship Load (0.2ms) SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = 1 AND "relationships"."followed_id" = 19 LIMIT 1
Rendered users/_unfollow.html.erb (3.7ms)
(0.2ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = 19
Rendered relationships/create.js.erb (7.7ms)
Completed 200 OK in 103ms (Views: 13.7ms | ActiveRecord: 6.0ms)
Started POST "/relationships" for 127.0.0.1 at 2011-11-30 21:08:51 -0800
Processing by RelationshipsController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sVoNM/yyDmn0czvbza+dxzLsPW4mPNlGxAI1vKN7Ez4=", "relationship"=>{"followed_id"=>"19"}, "commit"=>"Follow"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "19"]]
SQL (0.5ms) INSERT INTO "relationships" ("created_at", "followed_id", "follower_id", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Thu, 01 Dec 2011 05:08:51 UTC +00:00], ["followed_id", 19], ["follower_id", 1], ["updated_at", Thu, 01 Dec 2011 05:08:51 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 86ms
SQLite3::ConstraintException (columns follower_id, followed_id are not unique):
Rendered /home/anson/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)
Rendered /home/anson/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered /home/anson/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.2ms)
/home/anson/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.1/lib/active_record/connection_adapters/sqlite_adapter.rb:84: [BUG] Segmentation fault
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]
-- control frame ----------
c:0044 p:---- s:0205 b:0205 l:000204 d:000204 CFUNC :close
...
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
Aborted
有人可以帮忙吗?
代码如下。 应用程序/视图/用户/ _follow_form.html.erb
<% unless current_user?(@user) %>
<div id="follow_form">
<% if current_user.following?(@user) %>
<%= render 'unfollow' %>
<% else %>
<%= render 'follow' %>
<% end %>
</div>
<% end %>
应用程序/视图/用户/ _follow.html.erb
<%= form_for current_user.relationships.build(:followed_id => @user.id),
:remote => true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
应用程序/视图/用户/ _unfollow.html.erb
<%= form_for current_user.relationships.find_by_followed_id(@user),
:html => { :method => :delete },
:remote => true do |f| %>
<div class="actions"><%= f.submit "Unfollow" %></div>
<% end %>
应用程序/控制器/ relationships_controller.rb
class RelationshipsController < ApplicationController
before_filter :authenticate
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
应用程序/视图/关系/ create.js.erb
$("follow_form").update("<%= escape_javascript(render('users/unfollow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
应用程序/视图/关系/ destroy.js.erb
$("follow_form").update("<%= escape_javascript(render('users/follow')) %>")
$("followers").update('<%= "#{@user.followers.count} followers" %>')
答案 0 :(得分:0)
我遇到了同样的问题,包括服务器崩溃,并设法修复它: (请求确实已经发送了两次,可能是因为旧脚本) - 除了application.js之外基本上删除所有js文件(应该由rails new for rails 3.1生成)
# deleted: app/assets/javascripts/controls.js
# deleted: app/assets/javascripts/dragdrop.js
# deleted: app/assets/javascripts/effects.js
# deleted: app/assets/javascripts/prototype.js
# deleted: app/assets/javascripts/rails.js
此外,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)
检查您的公用文件夹中是否有Assets文件夹,其中包含application.js.不知道我是如何结束的,但这导致ajax查询被处理两次。