Issue upgrading from Mailboxer 0.11.0 to 0.12.4

156 views Asked by At

I have just updated to Mailboxer 0.12.4 and followed the instructions in the Github Readme. I had two controllers for working with the Gem

Notifications

class NotificationsController < ApplicationController
    before_action :signed_in_user, only: [:create]

    def new
      @user = User.find(:user)
      @message = current_user.messages.new
    end

   def create
      @recepient = User.find(params[:notification][:id])

      current_user.send_message(@recepient, params[:notification][:body],params[:notification][:subject])
      flash[:success] = "Message has been sent!"

      redirect_to user_path @recepient
   end
end

Conversations

class ConversationsController < ApplicationController
    before_action :signed_in_user

    def index
       @conversations = current_user.mailbox.conversations.paginate(page: params[:page],:per_page => 5)
    end

    def show
        @conversation = current_user.mailbox.conversations.find_by( :id => params[:id] )

        @receipts = @conversation.receipts_for(current_user).reverse!
    end
end

My users model has act_as_messagable. After the update this method in my users controller is throwing an error.

uninitialized constant UsersController::Notification

the code that is highlighted is

def show
    @user = User.find(params[:id])
    @message = Notification.new << this line
    ....
end

I have tried creating a Notification object in the console and I get the same error. I have read that the update has changed the namespacing but I don't know how to change my code to account for this.

This is the closest I have found to a solution but the guy doesn't say how he fixed it

1

There are 1 answers

0
Jeff Finn On BEST ANSWER

Ok I got this working I need to figure out why but it seems to be related to namespaces that were introduced in the upgrade to 0.12.4.

Step 1: Change my controllers to

mailboxer_notification_controller.rb

class MailboxerNotificationsController < ApplicationController
    before_action :signed_in_user, only: [:create]

    def new
        @user = User.find(:user)
        @message = current_user.messages.new
    end

    def create
       @recepient = User.find(params[:mailboxer_notification][:id])

       current_user.send_message(@recepient, params[:mailboxer_notification][:body],params[:mailboxer_notification][:subject])
       flash[:success] = "Message has been sent!"

       redirect_to user_path @recepient
   end
end

Note: that the param names needed to change

mailboxer_conversations_controller.rb

class MailboxerConversationsController < ApplicationController
    before_action :signed_in_user

    def index
        @conversations = current_user.mailbox.conversations.paginate(page: params[:page],:per_page => 5)
    end

    def show
        @conversation = current_user.mailbox.conversations.find_by( :id => params[:id] )

        @receipts = @conversation.receipts_for(current_user).reverse!
    end
end

Step 2: Anywhere I accessed a method belonging to theses controllers needed to be updated with the correct namespace

def show
    @user = User.find(params[:id])
    @message = Mailboxer::Notification.new

    ....
end

Step 3: Update your config/routes.rb

SampleApp::Application.routes.draw do
    resources :mailboxer_conversations
    resources :mailboxer_notifications, only: [:create]

    match '/sendMessage',   to: 'mailboxer_notifications#create',   via: 'post'

    match '/conversations', to: 'mailboxer_conversations#index',    via: 'get'
    match '/conversation',  to: 'mailboxer_conversations#show',     via: 'get'
    ....
end

I'm not sure of the exact reasons these fixes work, I need to spend more time reading about namespaces in rails. If anyone has a good explanation feel free to add to the answer