Search code examples
ruby-on-railsrubygemsmailboxer

Rails mailboxer gem, send message to user using form


I have installed the mailboxer gem to my rails app. It's successfully working when i'm using it in the console. Exemple: current_user.send_message(User.last, "Body", "subject")

But i want to know how to do to make it work with a form view and a controller. I want to be able to pass the send_message arguments through a view and send it to a message or a conversation controller.

I don't know the right way to handle this fantastic gem. Thanks in advance

J.D.


Solution

  • The controller to send a message from a form:

    class MessageController
      # GET /message/new
      def new
        # display form
      end
    
      # POST /message/create
      def create
        recipient = User.find(params[:recipient_id])
        current_user.send_message(recipient, params[:body], params[:subject])
      end
    end
    

    Form view:

    <%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
      <%= text_field_tag :subject %>
      <%= text_area_tag :body %>
      <%= submit_tag 'Send email' %>
    <% end %>
    

    A field for the recipient is missing in this example.