我一直在尝试自定义Mailboxer gem以满足我的目的。还有一些其他SO帖子引用了类似的功能,但我想问这个问题,因为它有些不同。
我重构了我的消息控制器:
def new
@user = User.find_by(id: params[:user])
end
def create
recipients = User.where(id: params['recipients'])
conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
flash[:success] = "Message has been sent!"
redirect_to conversation_path(conversation)
end
要:
def new
@user = User.find_by(id: params[:user])
@message = current_user.messages.new
@listing = Listing.find_by(id: params[:listing])
end
def create
@recipient = User.find_by(id: params[:user])
conversation = current_user.send_message(@recipient, "Hello", "Subject").conversation
flash[:notices] = ["Your message was successfully sent to the seller"]
redirect_to root_path
end
然后我又添加到messages / new.html.erb:
Send a message to
<%= @user.email %>
<%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
<%= hidden_field_tag(:listing, "#{@listing.id}") %>
<%= hidden_field_tag(:user, "#{@user.id}") %>
<%= submit_tag 'Send Message', class: "btn btn-primary" %>
<% end %>
这允许我访问我发送消息的用户对象以及列表ID,以便消息可以链接到列表。
Github回购:https://github.com/benhawker/rails_marketplace/tree/master/app
用户模型包括:acts_as_messageable
我没有在这里绘制任何错误,但在rails控制台中验证后,我希望发送消息的用户为@user.mailbox.conversations
如果有人试图用Mailboxer做同样的事情就能解释我哪里出错了会非常感激。
conversation = current_user.send_message(@recipient, "Hello", "Subject").conversation
我知道这一行可能很关键 - 在将参数传递给Mailboxer gem提供的.conversation
方法之后,我并不完全理解send_message
的目的。
答案 0 :(得分:1)
我想发布我的最终解决方案。我最终推出了自己的消息传递解决方案,从中期https://medium.com/@danamulder/tutorial-create-a-simple-messaging-system-on-rails-d9b94b0fbca1的优秀帖子中获得了很多灵感。
它远非完美,并且在查询控制器中有大量的重构需要发生,但它是有效的并且解决了我原来的问题。
使用以下关联创建了一个查询类。
class Inquiry < ActiveRecord::Base
belongs_to :listing
belongs_to :sender, :foreign_key => :sender_id, class_name: "User"
belongs_to :recipient, :foreign_key => :recipient_id, class_name: "User"
has_many :messages, dependent: :destroy, validate: false
accepts_nested_attributes_for :messages, reject_if: proc { |attributes| attributes["message"].blank? }
validates_presence_of :sender, :recipient, :listing
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
where("(inquiries.sender_id = ? AND inquiries.recipient_id =?) OR (inquiries.sender_id = ? AND inquiries.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
然后是与每个查询相关联的消息类。
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :inquiry
validates_presence_of :body, :inquiry_id, :user_id
end
然后创建了一个InquiriesController
class InquiriesController < ApplicationController
def index
@users = User.all
@inquiries = Inquiry.all
end
def new
@sender = User.find_by(id: params[:sender])
@recipient = User.find_by(id: params[:recipient])
@listing = Listing.find_by(id: params[:listing])
@inquiry = current_user.inquiries.new
@message = @inquiry.messages.build
end
def create
@sender = User.find_by(id: params[:sender_id])
@recipient = User.find_by(id: params[:recipient_id])
@listing = Listing.find_by(id: params[:listing_id])
if Inquiry.between(@sender,@recipient).present?
@inquiry = Inquiry.between(@sender, @recipient).first
else
@inquiry = Inquiry.create!(inquiry_params)
@inquiry.listing << Listing.find_by(id: params[:listing_id])
end
redirect_to inquiry_messages_path(@inquiry)
end
private
def inquiry_params
params.permit(:sender_id, :recipient_id, :listing_id)
end
在我的列表/展示页面中,我会通过发件人,收件人和列表ID来填充查询对象。
<%= link_to "Contact the Seller", inquiries_path(sender_id: current_user.id, recipient_id: @user.id, listing_id: @listing.id), method: 'post' %>
然后允许查询/索引:
<% @inquiries.each do |inquiry| %>
<% if inquiry.sender_id == current_user.id || inquiry.recipient_id == current_user.id %>
<% if inquiry.sender_id == current_user.id %>
<% recipient = User.find(inquiry.recipient_id) %>
<% else %>
<% recipient = User.find(inquiry.sender_id) %>
<% end %>
<div>
<%= link_to "Messages with #{recipient.email} about #{inquiry.listing.title}", inquiry_messages_path(inquiry)%>
</div>
<% end %>
<% end %>