我正在尝试实施一个关注/取消关注表单来创建/销毁用户与网站上的公司之间的关系。我在表单提交上呈现新的部分时遇到问题,将关系按钮从“关注”更改为“取消关注”
目前,我根据是否已存在关系呈现不同的部分:
`<% unless current_user.id == @company.user_id %>
<div id="#relationship">
<% if current_user.following?(@company) %>
<%= render :partial => 'unfollow' %>
<% else %>
<%= render :partial => 'follow' %>
<% end %>
</div>
<% end %>`
以下部分内容如下:
<%= form_for(current_user.relationships.build(followed_id: @company.id), remote: true) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow",class: "btn-transparent btn-hollow btn-huge" %>
<% end %>
虽然取消关注部分看起来像:
<%= form_for(current_user.relationships.find_by(followed_id: @company.id), html: { method: :delete }, remote: true) do |f| %>
<%= f.submit "Unfollow", class: "btn-transparent btn-hollow btn-huge" %>
<% end %>
这些在我的Relationships_Controller中使用Create和Destroy方法:
class RelationshipsController < ApplicationController
before_filter :authenticate_user!
def create
@company = Company.find(params[:relationship][:followed_id])
current_user.follow!(@company)
respond_to do |format|
format.html { redirect_to @company }
format.js {render layout: false}
end
end
def destroy
@company = Relationship.find(params[:id]).followed
current_user.unfollow!(@company)
respond_to do |format|
format.html { redirect_to @company }
format.js{render layout: false}
end
end
end
如果我设置remote:false,表单将按预期工作,创建和销毁关系,并且页面重新加载时按钮会更改。当我尝试通过设置remote:true来使用AJAX,并使用下面的代码进行关系/ create.js.erb和relationships / destroy.js.erb
$("#relationship").html("<%= j render('companies/unfollow') %>");
$("#relationship").html("<%= j render('companies/follow') %>");
但是,现在当我重新加载页面时 - 我可以单击按钮一次来创建/销毁关系对象。如果我再次点击,我会收到500错误。永远不会加载新的部分。
虽然我有点像菜鸟,但这个错误似乎指向我在chrome dev工具的jquery源代码中的这一行:
xhr.send( ( options.hasContent && options.data ) || null );
我正在使用Rails 4,Jquery-Turbolinks和Devise - 如果它们中的任何一个与问题无关。
现在令人难以置信的是,如果有人能提供帮助,那将非常感激!
更新
日志输出如下。第一个DELETE说它已经使我的部分,但它没有。第二个DELETE是第二次单击取消关注时发生的事情 - 它正确地指出与该ID号的关系不再存在,因为它在第一次操作时被删除。
Started DELETE "/relationships/429" for 127.0.0.1 at 2014-10-28 14:34:59 +0000
Processing by RelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "commit"=>"Unfollow", "id"=>"429"}
[1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1[0m
[1m[35mRelationship Load (0.4ms)[0m SELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = $1 LIMIT 1 [["id", 429]]
[1m[36mCompany Load (0.3ms)[0m [1mSELECT "companies".* FROM "companies" WHERE "companies"."id" = $1 LIMIT 1[0m [["id", 1]]
[1m[35mRelationship Load (0.3ms)[0m SELECT "relationships".* FROM "relationships" WHERE "relationships"."follower_id" = $1 AND "relationships"."followed_id" = 1 LIMIT 1 [["follower_id", 1]]
[1m[36m (0.2ms)[0m [1mBEGIN[0m
[1m[35mSQL (3.6ms)[0m DELETE FROM "relationships" WHERE "relationships"."id" = $1 [["id", 429]]
[1m[36m (0.5ms)[0m [1mCOMMIT[0m
Rendered companies/_follow.html.erb (2.5ms)
Rendered relationships/destroy.js.erb (5.3ms)
Completed 200 OK in 19ms (Views: 7.0ms | ActiveRecord: 6.3ms)
Started DELETE "/relationships/429" for 127.0.0.1 at 2014-10-28 14:35:04 +0000
Processing by RelationshipsController#destroy as JS
Parameters: {"utf8"=>"✓", "commit"=>"Unfollow", "id"=>"429"}
[1m[35mUser Load (0.9ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
[1m[36mRelationship Load (0.3ms)[0m [1mSELECT "relationships".* FROM "relationships" WHERE "relationships"."id" = $1 LIMIT 1[0m [["id", 429]]
Completed 404 Not Found in 4ms
ActiveRecord::RecordNotFound - Couldn't find Relationship with 'id'=429:
答案 0 :(得分:1)
您的身份证号码中有一个哈希值。
尝试更改
<div id="#relationship">
到
<div id="relationship">
答案 1 :(得分:0)
使用remote: true
时,您必须更改:
format.js{render layout: false}
与
format.js
否则:您永远不会呈现relationships/create.js.erb
和relationships/destroy.js.erb