gpt4 book ai didi

ruby-on-rails - 如何将收件箱邮件标记从未读更改为已读

转载 作者:行者123 更新时间:2023-12-03 15:17:46 25 4
gpt4 key购买 nike

我正在构建一个收件箱消息传递系统。我的所有消息都显示为未读,而大多数消息应标记为“已读”。我在我的消息模型中设置了它以检查是否未读或已读。所以我不明白为什么代码不适用于未读/已读。

消息.rb:

belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recepient,
:class_name => 'User',
:foreign_key => 'recepient_id'

def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id and self.id=id
self.recepient_deleted = true if self.recepient_id == user_id and self.id=id
self.sender_deleted && self.recepient_deleted ? self.destroy : save!
end

# Read message and if it is read by recepient then mark it is read
def self.readingmessage(id, reader)
message = find(id, :conditions => ["sender_id = ? OR recepient_id = ?", reader, reader])
if message.read_at.nil? && (message.recepient.user_id==reader)
message.read_at = Time.now
message.save!
end
message
end

# Based on if a message has been read by it's recepient returns true or false.
def read?
self.read_at.nil? ? false : true
end

def self.received_by(user)
where(:recepient_id => user.id)
end

def self.not_recepient_deleted
where("recepient_deleted = ?", false)
end

end

消息 Controller :

class MessagesController < ApplicationController

before_filter :set_user

def index
if params[:mailbox] == "sent"
@messages = @user.sent_messages
elsif params[:mailbox] == "inbox"
@messages = @user.received_messages
#elsif params[:mailbox] == "archieved"
# @messages = @user.archived_messages
end
end

def new
@message = Message.new
if params[:reply_to]
@reply_to = User.find_by_user_id(params[:reply_to])
unless @reply_to.nil?
@message.recepient_id = @reply_to.user_id
end
end
end

def create
@message = Message.new(params[:message])
@message.sender_id = @user_id
if @message.save
flash[:notice] = "Message has been sent"
redirect_to user_messages_path(current_user, :mailbox=>:inbox)
else
render :action => :new
end
end

def show
@message = Message.find(params[:id])
end

def delete_multiple
if params[:delete]
params[:delete].each { |id|
@message = Message.find(id)
@message.mark_message_deleted(@message.id,@user_id) unless @message.nil?
}
flash[:notice] = "Messages deleted"
end
redirect_to user_messages_path(@user, @messages)
end

private
def set_user
@user = current_user
end
end

索引 View :

<h2>Your Inbox</h2>
<% if @messages.size == 0 %>
No messages in your Inbox
<% end %>
<% else %>
<%= form_tag delete_multiple_user_messages_path, :method=>:post do %>
<table class="table table-bordered">
<tr>
<th>Delete?</th>
<th>Sent</th>
<th>Sender</th>
<th>Sent</th>
</tr>
<% for message in @messages %>
<tr>
<td><%= check_box_tag "delete[]", message.id %></td>
<td>
<% if message.read? %>
<%= link_to h(message.subject), user_message_path(@user, message) %>
<% else %>
<%= link_to "#{h(message.subject)} (unread)", user_message_path(@user, message) %>
<% end %>
</td>

<td><%=h message.created_at.to_s(:long) %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Delete selected" %> |
<% end %>
<%= link_to "Compose New Message", new_user_message_path(@user)%> |
<%= link_to "View Sent Messages", user_messages_path(current_user, :mailbox => :sent)%>

最佳答案

您的消息都显示为未读,因为 read_at 字段从未从 nil 更改。

看起来您的 readingmessage 方法应该更改值,但它从未被调用过。

(在 Controller 的 show 方法中调用它是有意义的。)

型号:

def readingmessage
self.read_at ||= Time.now
save
end

Controller :

  def show
@message = Message.find(params[:id])
@message.readingmessage if @message.recipient == current_user
end

关于ruby-on-rails - 如何将收件箱邮件标记从未读更改为已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15792223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com