我有以下私人消息模型
belongs_to :from_user, class_name: 'User'
belongs_to :to_user, class_name: 'User'
belongs_to :thread, class_name: 'Message', foreign_key: 'parent_message_id'
has_many :replies, class_name: 'Message'
attr_accessible :subject, :body
scope :original_message, where("parent_message_id IS NULL")
我已经设置了所有设置的视图以将消息显示为线程,我想在底部放置一个快速回复表单。我在设置to_user的最佳方式上完全冻结了大脑,因为线程中的任何给定消息可能属于当前用户或另一端的用户。有什么建议吗?
答案 0 :(得分:0)
我有类似的要求,我做的是添加了javascript来根据点击线程的“回复”按钮移动回复textarea。
在回复时,您设置当前用户回复的帖子的变量,并根据您可以添加to_user的帖子的所有者
如果它是平坦的对话(如在论坛中)或像railscasts.com中的线索对话,这应该有效。
答案 1 :(得分:0)
如果线程中只有两个用户,那么我们可以假设from_user
或to_user
是current_user
,这意味着不是current_user
的用户另一方。所以:
orig_msg = @thread.messages.original_message
if orig_msg.from_user == current_user
other_user = orig_msg.to_user
else
other_user = orig_msg.from_user
end
或者:
other_user = orig_msg.to_user
other_user = orig_msg.from_user unless orig_msg.from_user == current_user
或者:
other_user = orig_msg.from_user == current_user ?
orig_msg.to_usr : orig_msg.from_user