ruby-on-railsrubyruby-on-rails-4rubygemsmailboxer

Mailboxer gem active record error on the conversations page


I'm trying to setup the mailboxer gem. It is mostly setup but I get the following error when I send a message and it tries to display the conversations page

Couldn't find Mailboxer::Conversation with 'id'=2 [WHERE "mailboxer_notifications"."type" = 'Mailboxer::Message' AND "mailboxer_receipts"."receiver_id" = ? AND "mailboxer_receipts"."receiver_type" = ?]

it is highlighting this line of coding in conversations controller as the error point

 def get_conversation
    @conversation ||= @mailbox.conversations.find(params[:id])
 end

here is the conversations controller

class ConversationsController < ApplicationController
  before_action :authenticate_user
  before_action :get_mailbox
  before_action :get_conversation, except: [:index]
  before_action :get_box, only: [:index]
  before_action :get_conversation, except: [:index, :empty_trash]

 def index
    @user = current_user
  if @box.eql? "inbox"
    @conversations = @mailbox.inbox
  elsif @box.eql? "sent"
    @conversations = @mailbox.sentbox
  else
    @conversations = @mailbox.trash
  end

  @conversations = @conversations.paginate(page: params[:page], per_page: 10)
end

  def show
    @user = current_user
  end

  def reply
  @user = current_user
  current_user.reply_to_conversation(@conversation, params[:body])
  flash[:success] = 'Reply sent'
  redirect_to user_conversation_path
end


def destroy
  @user = current_user
  @conversation.move_to_trash(current_user)
  flash[:success] = 'The conversation was moved to trash.'
  redirect_to user_conversations_path
end

def restore
  @user = current_user
  @conversation.untrash(current_user)
  flash[:success] = 'The conversation was restored.'
  redirect_to user_conversations_path
end


def empty_trash
  @user = current_user
  @mailbox.trash.each do |conversation|
    conversation.receipts_for(current_user).update_all(deleted: true)
  end
  flash[:success] = 'Your trash was cleaned!'
  redirect_to user_conversations_path
end

def mark_as_read
  @user = current_user
  @conversation.mark_as_read(current_user)
  flash[:success] = 'The conversation was marked as read.'
  redirect_to user_conversations_path
end

  private

  def get_box
  if params[:box].blank? or !["inbox","sent","trash"].include?(params[:box])
    params[:box] = 'inbox'
  end
  @box = params[:box]
end

  def get_mailbox
    @mailbox ||= current_user.mailbox
  end

    def authenticate_user
    unless ((current_user.id = params[:user_id]) unless current_user.nil?)
        #if logged in go to their messages page else go to login path
      flash[:error] = 'Looks like your not supposed to be there'
      redirect_to login_path

    end
  end


  def get_conversation
    @conversation ||= @mailbox.conversations.find(params[:id])
 end

end

I mostly followed this tutorial in setting the mailboxer gem up

I'm not sure why I'm getting the error or what I can do to fix it.

update

New error of

app/views/conversations/show.html.erb where line #19 raised:

No route matches {:action=>"reply", :controller=>"conversations", :id=>nil, :user_id=>"example-user"} missing required keys: [:id]


Parameters:

{"user_id"=>"example-user",
 "id"=>"4"}

conversations/show.html.erb

<% provide(:title, "Your Conversations") %>

<div class="panel panel-default">
  <div class="panel-heading"><%= @conversation.subject %></div>

  <div class="panel-body">
    <div class="messages">
      <% @conversation.receipts_for(current_user).each do |receipt| %>
        <% message = receipt.message %>

         <%= message.sender.username %>
          says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %>
        <%= message.body %>
      <% end %>
    </div>
  </div>
</div>

<%= form_tag reply_user_conversation_path(@user, @conversation), method: :post do %> <!-- line 19 -->
  <div class="form-group">
    <%= text_area_tag 'body', nil, cols: 3, class: 'form-control', placeholder: 'Type something...', required: true %>
  </div>
  <%= submit_tag "Send Message", class: 'btn btn-primary' %>
<% end %>

Solution

  • The error(Couldn't find Mailboxer::Conversation with 'id'=2) you get because the id = 2 of conversions does not exists.

    so you can modify your error method something like below

    def get_conversation
        @conversation = @mailbox.conversions.new || @mailbox.conversations.find(params[:id])
     end