[英]Ruby on rails 3 friendship
我正在尝试通过以下链接构建友谊系统: 如何在Rails 3中为社交网络应用程序实现友谊模型? 。 但是,代码似乎非常过时,我可能会对其进行更改,而我试图做的atm只是建立一个关系,但这似乎行不通。
所以这是我的创作
#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 %>
您需要将@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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.