我正在尝试按照以下链接建立友谊系统:How to Implement a Friendship Model in Rails 3 for a Social Networking Application?。然而,代码似乎非常过时,我可能会改变它,我试图做的只是创建一个关系,但这似乎不起作用。
所以我的创建
#Send a friendship request
def create
Friendship.request(@customer, @friend)
redirect_to friendships_path
end
然后技术上会调用位于模型中的方法请求,该请求已在前一篇文章中实现。
def self.request(customer, friend)
unless customer == friend or Friendship.exists?(customer, friend)
transaction do
create(:customer => customer, :friend => friend, :status => 'pending')
create(:customer => friend, :friend => customer, :status => 'requested')
end
end
end
我还将这些添加到模型中
attr_accessible :status, :customer_id, :friend_id, :customer, :friend
然而友谊并没有被创造出来。有什么理由不?我称关系已经跟随
<%= link_to "Add friend", friendships_path(:friend_id => customer), :method => :post %>
答案 0 :(得分:0)
您需要将@customer和@friend分开。在您的链接中,您正在为客户设置:friend_id,而您永远不会设置@customer ID。
试试这个:
def create
@customer = current_account
@friend = Account.find(params[:friend_id])
Friendship.request(@customer, @friend)
redirect
end
在link_to中,您需要:
<%= link_to "Add Friend", friendships_path(:friend_id => friend),: method => :post %>