当用户撰写邮件时,如何设置它以引用为Messages表中的conversation_id
创建的新message_id?
例如,用户A向用户B发送新消息。已创建Message_id 26并且conversation_id将为26。
show.html.erb:
<h4>Send Message To User</h4>
<p><%= link_to "Message Me", new_user_message_path(@user), :class => "button" %>
new.html.erb:
<%= f.text_field :conversation_id %>
控制器:
def index
@messages = Message.scoped
@message = Message.new
if params[:mailbox] == "sent"
@messages = @user.sent_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
elsif params[:mailbox] == "inbox"
@messages = @user.received_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
#elsif params[:mailbox] == "archived"
# @messages = @user.archived_messages
end
if params[:mailbox] == "unread"
@messages = @user.unread_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
if params[:mailbox] == "trash"
@messages = @user.deleted_messages.paginate :per_page => 10, :page => params[:page], :order => "created_at DESC"
end
end
def new
@message = Message.new
@message.conversation_id = params[:conversation_id]
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
@new_message = Message.new
@message = Message.find(params[:id])
@message.readingmessage if @message.recipient == current_user
end
def reply
@reply_message = Message.new
@message = Message.new
@message.conversation_id = params[:conversation_id]
end
def destroy
@message = Message.find(params[:id])
@message.destroy
flash[:notice] = "Successfully deleted message."
redirect_to user_messages_path(@user, @messages)
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
def update
@message = Message.new
if params[:reply_to]
@reply_to = User.find_by_id(params[:reply_to])
unless @reply_to.nil?
@message.recipient_id = @reply_to.id
end
end
end
型号:
class Message < ActiveRecord::Base
attr_accessible :subject, :conversation_id, :body, :parent_id, :sender_id, :recipient_id, :read_at,:sender_deleted,:recipient_deleted
validates_presence_of :subject, :message => "Please enter message title"
has_many :notifications, as: :event
belongs_to :conversation, inverse_of: :messages
belongs_to :user
scope :unread, -> {where('read_at IS NULL')}
scope :not_deleted_by_recipient, where('messages.recipient_deleted IS NULL OR messages.recipient_deleted = ?', false)
scope :not_deleted_by_sender, where('messages.sender_deleted IS NULL OR messages.sender_deleted = ?', false)
belongs_to :sender,
:class_name => 'User',
:foreign_key => 'sender_id'
belongs_to :recipient,
:class_name => 'User',
:foreign_key => 'recipient_id'
after_create :set_converstation_id
def set_conversation_id
update_column :conversation_id, id
end
def reply
new_message.reply_from_user_id = self.id #save the user id of original repost, to keep track of where it originally came from
end
def self.by_date
order("created_at DESC")
end
# marks a message as deleted by either the sender or the recipient, which ever the user that was passed is.
# When both sender and recipient marks it deleted, it is destroyed.
def mark_message_deleted(id,user_id)
self.sender_deleted = true if self.sender_id == user_id
self.recipient_deleted = user_id if self.recipient_id == user_id
(self.sender_deleted > 0 && self.recipient_deleted > 0) ? self.destroy : self.save!
(self.sender_deleted != 0 && self.recipient_deleted != 0)
end
# Read message and if it is read by recipient then mark it is read
def readingmessage
self.read_at ||= Time.now
save
end
# Based on if a message has been read by it's recipient returns true or false.
def read?
self.read_at.nil? ? false : true
end
def self.received_by(user)
where(:recipient_id => user.id)
end
def self.not_recipient_deleted
where("recipient_deleted = ?", false)
end
def self.sent_by(user)
Message.where(:sender_id => user.id)
end
def next(same_recipient = true)
collection = Message.where('id <> ? AND created_at > ?', self.id, self.created_at).order('created_at ASC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
def previous(same_recipient = true)
collection = Message.where('id <> ? AND created_at < ?', self.id, self.created_at).order('created_at DESC')
collection.where(recipient_id: self.recipient_id) if same_recipient
collection.first
end
end
private
def send_notification(message)
message.notifications.create(user: message.recipient)
end
答案 0 :(得分:1)
如果您没有单独的会话和消息模型,您将如何区分消息的创建和对话的创建?
您应该拥有一个has_many
消息并引用这两个用户的会话模型。
无论如何,您要求的是在创建全新消息(或对话)时拥有conversation_id
?
您可以通过两种方式执行此操作:
1.-更改
@message.conversation_id = params[:conversation_id]
到
@message.conversation_id = @message.id
在视图中,为hidden_field_tag
conversation_id
(或执行此操作的帮助程序)
2.-直接在create方法
上设置此ID@message.conversation_id = @message.id
(这是你想要的行为吗?)
另外,为什么你有@new_message
和@message
?
正如评论中所述:
我有这个
<%= link_to "Reply", reply_user_messages_path(@message.sender, :conversation_id => @message) %>
<%= link_to "Reply", reply_user_messages_path(@message.sender, :conversation_id => @message.conversation_id) %>
我需要找到一种方法来合并它们中的两个。一种方法设置conversation_id,另一种方法复制它。
<%= link_to "Reply", reply_user_messages_path(@message.sender, conversation_id: @message.conversation_id ? @message.conversation_id : @message.id) %>