直接链接以向用户发送消息Rails

时间:2018-01-24 11:28:49

标签: ruby-on-rails devise link-to mailboxer

我是Ror的新人, 我想在我的应用程序中添加我的产品展示:

  • 允许客户向产品销售商发送新消息的链接。

创建的新邮件应包括:

  • 主题
  • 中的product.id
  • 文字区域
  • 收件人卖家名称(隐藏所有其他用户)
  • 提交按钮以发送消息。 我为我的用户使用了devise,为消息传递功能使用了gem mailboxer。

我没有成功创建正确的链接,也无法隐藏所有用户,只能将消息发送给产品销售商。

保留/ _form.html.erb:

<% if user_signed_in? && current_user != @user %>
<%= link_to "Contact seller", conversations_path(sender_id: current_user.id, recipient_id: @user.id), method: 'post', class: 'btn btn-md btn-danger' %>
<% end %>

messages.new.html.erb

<div class="container-fluid">
  <div class="row">
    <div class="col-md-8 col-md-offset-2 text-center">

    <h3>Send a message</h3>

      <%= form_tag messages_path, method: :post do %>
      <div class="form-group">
        <%= label_tag 'message[subject]', 'Subject' %>
        <%= text_field_tag 'message[subject]', nil, class: 'form-control', required: true %>
      </div>

      <div class="form-group">
        <%= label_tag 'message[body]', 'Message' %>
        <%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
      </div>

      <div class="form-group">
        <%= label_tag 'recipients', 'Choose a recipient' %>
        <%= select_tag 'recipients', recipients_options(@chosen_recipient), multiple: true, class: 'form-control chosen-it' %>
      </div>

      <%= submit_tag 'Send message', class: 'btn btn-md btn-danger' %>
      <% end %>
      <br>
      <br>
    </div>
  </div>
</div>

messages_controller.rb:

class MessagesController < ApplicationController
  before_action :authenticate_user!

def new
  @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
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
end

routes.rb中:

  resources :conversations, only: [:index, :show, :destroy] do
    member do
      post :reply
      post :restore
      post :mark_as_read
    end
    collection do
      delete :empty_trash
    end
  end

  resources :messages, only: [:new, :create]

0 个答案:

没有答案