【发布时间】:2012-08-22 07:23:43
【问题描述】:
我正在尝试通过以下链接建立一个友谊系统:How to Implement a Friendship Model in Rails 3 for a Social Networking Application?。但是代码似乎非常过时,我可能会更改它,我正在尝试做的 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 %>
【问题讨论】:
-
我正在尝试自己实现一个友谊模型,但我对应该在 attr_accessible 中添加什么感到困惑。我认为状态应该是那里的唯一属性。是否存在恶意用户更改 id 从而改变用户之间友谊的风险?
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1