Mehdi Mehrtash

Joined

500 Experience
4 Lessons Completed
0 Questions Solved

Activity

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

Posted in How do I add multiple attachments to Mailboxer?

I am using the Mailboxer gem. I have enabled multiple file uploads for the attachment field. What is the best way to set up my controller so it saves all the attachments? Would making a new model called "message_attachment" be a good idea? In that case, how do I set up the message model (which I currently don't have in my model folder) so I can include "has_many message_attachments"?

show.html.haml
= form_for @message, url: conversation_messages_path(@conversation) do |f|
= f.text_area :body, class: "form-control"
-# = f.file_field :attachment
= f.file_field :attachment, multiple: :true, name: "message_attachments[photo][]"
.yellow-btn
= f.submit "Send", class: "submit"

messages_controller.rb
def create
receipt = current_user.reply_to_conversation(@conversation, params[:mailboxer_message][:body], nil, true, true, params[:mailboxer_message][:attachment])
redirect_to conversation_path(@conversation)
end

Thank you very much!