You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails新手求助:实现商品详情页联系卖家的定向消息功能

Fixing Mailboxer Directed Messaging to Sellers in Rails

Hey there! You're already off to a good start with Devise and Mailboxer—let's tweak a few parts to get that seller-specific messaging working exactly how you want it.

Your current link points to the conversations path, but we need to send users to the new message page instead, while passing along the seller ID and product ID. Update your reservations/_form.html.erb link:

<% if user_signed_in? && current_user != @user %>
  <%= link_to "Contact seller", new_message_path(to: @user.id, product_id: @product.id), class: 'btn btn-md btn-danger' %>
<% end %>
  • We use new_message_path (maps to messages#new) instead of the conversations route—this is where we'll build the message.
  • Pass to (the seller's ID) and product_id (to auto-fill the subject) as query params.
  • No need for method: 'post' here since we're just loading a form page (GET request).

2. Update the New Message Form to Hide Recipients & Auto-Fill Subject

We want to remove the recipient dropdown entirely (since we're only messaging the seller) and pre-populate the subject with the product ID. Modify 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 to Seller</h3>
      <%= form_tag messages_path, method: :post do %>
        <div class="form-group">
          <%= label_tag 'message[subject]', 'Subject' %>
          <%= text_field_tag 'message[subject]', "Inquiry about Product ##{params[:product_id]}", 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>
        <!-- Hidden field to pass the seller's ID without showing it to the user -->
        <%= hidden_field_tag 'recipients[]', @chosen_recipient.id %>
        <%= submit_tag 'Send message', class: 'btn btn-md btn-danger' %>
      <% end %>
      <br>
      <br>
    </div>
  </div>
</div>
  • The subject field auto-fills with a message that includes the product ID, pulled directly from the URL params.
  • We replace the recipient dropdown with a hidden field that passes the seller's ID (we'll grab this in the controller).
  • Updated the page heading to make it clear who the user is messaging.

3. Tweak the Messages Controller for Reliability

We'll make sure we're fetching the seller correctly in the new action, and add a quick check in create to avoid sending messages to non-existent users:

class MessagesController < ApplicationController
  before_action :authenticate_user!

  def new
    # Grab the seller from the 'to' param passed in the link
    @chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
    # Optional: Store product ID if you want to use it elsewhere in the view
    @product_id = params[:product_id]
  end

  def create
    recipients = User.where(id: params['recipients'])
    if recipients.present?
      # Send the message using Mailboxer's send_message method
      conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation
      flash[:success] = "Message has been sent!"
      redirect_to conversation_path(conversation)
    else
      flash[:error] = "Oops, we couldn't find that recipient!"
      redirect_back fallback_location: root_path
    end
  end
end

4. Confirm Your Routes Are Set Correctly

Your existing route for messages is perfect—no changes needed here:

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

Test It Out!

  1. Log in as a user who isn't the product seller.
  2. Navigate to the product detail page and click "Contact seller".
  3. You'll see a form with the subject already filled in with the product ID, no recipient dropdown, and a message body field.
  4. Type your message and hit send—it'll go straight to the seller.

That's all! This setup hits every requirement you listed: subject includes the product ID, text input for your message, only the seller receives it (no other users visible), and a working send button.

内容的提问来源于stack exchange,提问作者SamyCha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:24:58