Messaging with Rails and Mailboxer

Great tutorial, thank you for writing. Though I ran into two issues.

  1. I get an error when sending messages: wrong number of arguments (1 for 0)
    It points to this line in the messages_controller:
    conversation = current_user.send_message(recipients, params[:message][:body], params[:message][:subject]).conversation

Here is a stack trace:

Started POST "/messages" for 10.0.2.2 at 2015-10-10 16:23:39 +0000
Processing by MessagesController#create as HTML
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"YiqT94X0gvp2W9aT1PNTx6iRM3E40ozMlkgZsU6LGpfoBxTygHFdBSkeyUYHHwGwWX+x8QpjJTEj4HYyrqhxng==", "message"=>{"subject"=>"work", "body"=>"pleas work this time!"}, "recipients"=>["2"], "commit"=>"Send"}
User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2
(0.2ms)  BEGIN
SQL (0.7ms)  INSERT INTO "mailboxer_conversations" ("subject", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["subject", "work"], ["created_at", "2015-10-10 16:23:39.424583"], ["updated_at", "2015-10-10 16:23:39.424583"]]
SQL (0.3ms)  INSERT INTO "mailboxer_notifications" ("type", "sender_id", "sender_type", "body", "subject", "created_at", "updated_at", "conversation_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["type", "Mailboxer::Message"], ["sender_id", 1], ["sender_type", "User"], ["body", "pleas work this time!"], ["subject", "work"], ["created_at", "2015-10-10 16:23:39.424583"], ["updated_at", "2015-10-10 16:23:39.424583"], ["conversation_id", 10]]
SQL (0.2ms)  INSERT INTO "mailboxer_receipts" ("mailbox_type", "receiver_id", "receiver_type", "notification_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["mailbox_type", "inbox"], ["receiver_id", 2], ["receiver_type", "User"], ["notification_id", 12], ["created_at", "2015-10-10 16:23:39.436761"], ["updated_at", "2015-10-10 16:23:39.436761"]]
(0.8ms)  COMMIT
(0.3ms)  BEGIN
SQL (0.2ms)  INSERT INTO "mailboxer_receipts" ("mailbox_type", "receiver_id", "receiver_type", "is_read", "notification_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["mailbox_type", "sentbox"], ["receiver_id", 1], ["receiver_type", "User"], ["is_read", "t"], ["notification_id", 12], ["created_at", "2015-10-10 16:23:39.441001"], ["updated_at", "2015-10-10 16:23:39.441001"]]
(0.5ms)  COMMIT
(0.2ms)  SELECT COUNT(*) FROM "mailboxer_conversation_opt_outs" WHERE "mailboxer_conversation_opt_outs"."conversation_id" = $1 AND "mailboxer_conversation_opt_outs"."unsubscriber_type" = $2 AND "mailboxer_conversation_opt_outs"."unsubscriber_id" = $3  [["conversation_id", 10], ["unsubscriber_type", "User"], ["unsubscriber_id", 2]]
Completed 500 Internal Server Error in 23ms (ActiveRecord: 3.8ms)

ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/messages_controller.rb:9:in `create'

Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.5ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (23.1ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.3ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (12.8ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
Rendered /usr/local/rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (27.3ms)
  1. I am getting the “Resource Not Found” message when I try to empty the trash.

    Started DELETE “/conversations/empty_trash” for 10.0.2.2 at 2015-10-10 16:21:49 +0000
    Processing by ConversationsController#empty_trash as HTML
    Parameters: {“authenticity_token”=>“mhCTp4FfbBlr573VGWT8PKM16Lu5pAKo93nHVfNMF075wWvnIUMkpO+ipat7rsP3dLNWf7WyYmXxphcQK6C+0w==”}
    User Load (0.3ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 2]]
    Redirected to http://localhost:3000/conversations?box=trash
    Completed 302 Found in 4ms (ActiveRecord: 0.3ms)

    Started GET “/conversations?box=trash” for 10.0.2.2 at 2015-10-10 16:21:49 +0000
    Processing by ConversationsController#index as HTML
    Parameters: {“box”=>“trash”}
    User Load (0.3ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 ORDER BY “users”.“id” ASC LIMIT 1 [[“id”, 2]]
    (0.4ms) SELECT DISTINCT COUNT(DISTINCT “mailboxer_conversations”.“id”) FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“mailbox_type” = $1 AND “mailboxer_receipts”.“trashed” = $2 AND “mailboxer_receipts”.“deleted” = $3 AND “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $4 AND “mailboxer_receipts”.“receiver_type” = $5 AND “mailboxer_receipts”.“is_read” = $6 [[“mailbox_type”, “inbox”], [“trashed”, “f”], [“deleted”, “f”], [“receiver_id”, 2], [“receiver_type”, “User”], [“is_read”, “f”]]
    (0.8ms) SELECT DISTINCT COUNT(DISTINCT “mailboxer_conversations”.“id”) FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Conversation Load (0.7ms) SELECT DISTINCT “mailboxer_conversations”.* FROM “mailboxer_conversations” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“conversation_id” = “mailboxer_conversations”.“id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) INNER JOIN “mailboxer_receipts” ON “mailboxer_receipts”.“notification_id” = “mailboxer_notifications”.“id” WHERE “mailboxer_notifications”.“type” = ‘Mailboxer::Message’ AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 ORDER BY mailboxer_conversations.updated_at DESC LIMIT 10 OFFSET 0 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    (0.7ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 9 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.4ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 9]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 11]]
    User Load (0.2ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    User Load (0.2ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.2ms)
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 9]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 8 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 8]]
    Mailboxer::Receipt Load (0.1ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 10]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.6ms)
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 8]]
    (0.3ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 7 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 7]]
    Mailboxer::Receipt Load (0.1ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 9]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.2ms)
    Mailboxer::Message Load (0.4ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 7]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 6 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 6]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 8]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.1ms)
    Mailboxer::Message Load (0.5ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 6]]
    (0.4ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 4 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 4]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 4]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    Rendered conversations/_participants.html.erb (5.2ms)
    Mailboxer::Message Load (0.2ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 4]]
    (0.5ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 2 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 2]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (6.2ms)
    Mailboxer::Message Load (0.6ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 2]]
    (0.7ms) SELECT COUNT() FROM “mailboxer_receipts” INNER JOIN “mailboxer_notifications” ON “mailboxer_notifications”.“id” = “mailboxer_receipts”.“notification_id” AND “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) WHERE “mailboxer_notifications”.“conversation_id” = 1 AND “mailboxer_receipts”.“receiver_id” = $1 AND “mailboxer_receipts”.“receiver_type” = $2 AND “mailboxer_receipts”.“trashed” = $3 AND “mailboxer_receipts”.“deleted” = $4 [[“receiver_id”, 2], [“receiver_type”, “User”], [“trashed”, “t”], [“deleted”, “f”]]
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.
    FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at LIMIT 1 [[“conversation_id”, 1]]
    Mailboxer::Receipt Load (0.2ms) SELECT “mailboxer_receipts”.* FROM “mailboxer_receipts” WHERE “mailboxer_receipts”.“notification_id” = $1 [[“notification_id”, 1]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 2]]
    CACHE (0.0ms) SELECT “users”.* FROM “users” WHERE “users”.“id” = $1 LIMIT 1 [[“id”, 1]]
    Rendered conversations/_participants.html.erb (5.7ms)
    Mailboxer::Message Load (0.3ms) SELECT “mailboxer_notifications”.* FROM “mailboxer_notifications” WHERE “mailboxer_notifications”.“type” IN (‘Mailboxer::Message’) AND “mailboxer_notifications”.“conversation_id” = $1 ORDER BY created_at DESC LIMIT 1 [[“conversation_id”, 1]]
    Rendered conversations/_conversation.html.erb (163.7ms)
    Rendered conversations/index.html.erb within layouts/application (186.6ms)
    Rendered layouts/_nav.html.erb (3.0ms)
    Rendered layouts/_alert.html.erb (0.7ms)
    Completed 200 OK in 637ms (Views: 621.1ms | ActiveRecord: 12.2ms)

If you need any more information, please let me know. I have been staring at this code for days now trying to figure out what’s wrong.

Thanks