我在一个社交网站上工作(老实说,基本上是一个 facebook 的副本......),我重用了 insoshi 的大部分内容。但是 insoshi 的供稿对我的喜好不够准确。因为它不支持更专业的消息。您将在以下代码中看到我的意思:
item = activity.item
relationship = relationship(item)
case relationship
when 1
raw %(<p>You wrote on your own wall: <br/>
#{truncate(item.body, :length => 20)}</p>)
when 2
raw %(<p>#{link_to item.user.name, item.user} wrote on your wall</p>)
when 3
raw %(<p>#{link_to item.user.name, item.user} wrote on his wall</p>)
when 4
raw %(<p>You wrote on #{link_to item.user.name, item.user}'s wall</p>)
when 5
raw %(<p>#{link_to item.user.name, item.user} wrote on
#{link_to item.contact.name, item.contact}'s wall</p>)
end
def relationship(item)
unless item.owner.nil?
contact = item.owner #so that it works for posts as well
else
contact = item.contact
end
user = item.user
if current_user != contact or current_user != user
return 5
else
if current_user == contact
if current_user == user
return 1
else
return 2
end
else
if contact == user
return 3
else
return 4
end
end
end
end
我有不同类型的物品。通常项目有一个“用户”和一个“联系人”。除了帖子,它们还有一个“用户”和一个“所有者”。因为帖子的其他人可以将其写在某人的墙上(因此为所有者)。
现在,当我尝试将联系人设置为 item.contact 时,问题就出现了......它只是不断地用“NoMethod”错误来困扰我,说 item.contact 不存在。(如果该项目是帖子而不是“连接”或类似项目,则这一点很明显)。
所以我征求你的意见:1)用更多的红宝石解决问题,或者2)改变帖子模型,让帖子有一个“用户”和一个“联系人”?
谢谢大家斯特凡诺