Skip to main content

Mailboxer: How to attach a file when starting a new conversation

Rails • Asked by Mehdi Mehrtash

I am using the Mailboxer gem. When creating a new conversation, the message that is created at that point is does not attach the file I am uploading.

new.html.haml
= form_tag conversations_path, method: :post do
= select_tag :user_id, options_from_collection_for_select(@recipients, :id, :first_name)
= text_field_tag :subject, nil, placeholder: "Subject", class: "form-control"
= text_area_tag :body, nil, placeholder: "Leave a comment", class: "form-control"
= file_field_tag :attachment
= submit_tag "Send"

conversations_controller.rb
def create
recipient = User.find(params[:user_id])
receipt = current_user.send_message(recipient, params[:body], params[:subject], true, params[:attachment], Time.now)
redirect_to conversation_path(receipt.conversation)
end

byebug 

** params**
{"utf8"=>"✓", "authenticity_token"=>"R/wL/Zs4PTWJqHl5o3qHHnhhh2FewSymus3L0Svpdg0b9u1zcKTgnyGy4hJdGYhdNPU/hvL8Mq/dvNOUUCzXJg==", "user_id"=>"8", "subject"=>"Conversation Subject", "body"=>"Conversation Body", "attachment"=>"cupcake1.jpg", "commit"=>"Send", "controller"=>"conversations", "action"=>"create"}

** receipt.message**
#

According to the documentation, that is the order of the send_message method
** def send_message(recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)**

Thank you for your help


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.