我正在遵循某个教程来使用邮箱程序创建用户-用户消息传递系统。
问题是,我希望一个用户能够在另一用户的页面(收件人)上创建对话,而不必从收件人列表中选择对话。
到目前为止,这是我已经完成的工作,我在用户的显示页面上添加了一个表单部分用于对话,但是我试图删除必须选择收件人的那一部分,而我希望由params[:id]
设置收件人
有什么想法或建议吗?也许有办法将下面的选择的默认值设置为params[:id]
吗?
<%= form_for :conversation, url: :conversations, html: { class: "" } do |f| %>
<div class="form-group">
<%= f.label :recipients %>
<p id="recipient-select"><%= f.select(:recipients, @users.all.collect {|p| [ p.username, p.id ] }, {}, { multiple: true}) %></p>
</div>
<div class="form-group">
<%= f.label :subject %>
<%= f.text_field :subject, placeholder: "Subject", class: "form-control", :required => true %>
</div>
<div class="form-group">
<%= f.label :message %>
<%= f.text_area :body, class: 'form-control',placeholder: "Type your message here", rows: 4 %>
</div>
<%= f.submit "Send Message", class: "btn btn-success" %>
<% end %>
会话控制器
class ConversationsController < ApplicationController
before_action :authenticate_user!
def create
recipients = User.where(id: conversation_params[:recipients])
recipients.each do |recipient|
if recipient != current_user
@recipient = recipient
end
end
conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
flash[:success] = "Your message was successfully sent!"
redirect_to conversation_path(conversation)
end
def show
@receipts = conversation.receipts_for(current_user)
conversation.mark_as_read(current_user)
end
def reply
current_user.reply_to_conversation(conversation, message_params[:body])
flash[:notice] = "Your reply message was successfully sent!"
redirect_to conversation_path(conversation)
end
private
def message_params
params.require(:message).permit(:body, :subject)
end
def conversation_params
params.require(:conversation).permit(:subject, :body,recipients:[])
end
end