[英]Customising the Mailboxer gem - Rails 4 - Send message linked to a listing
我一直在嘗試自定義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
為@user.mailbox.conversations
如果嘗試使用Mailboxer進行相同操作的任何人都能解釋我要去哪里,那將非常感謝。
conversation = current_user.send_message(@recipient, "Hello", "Subject").conversation
我知道這行可能是關鍵-在將參數傳遞給Mailboxer gem提供的send_message
方法后,我不完全了解.conversation
的目的。
我想為此發布最終的解決方案。 我最終從我在Medium 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 %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.