对话页面

时间:2016-03-29 06:03:06

标签: ruby-on-rails ruby ruby-on-rails-4 rubygems mailboxer

我正在尝试设置邮箱gem。它主要是设置,但是当我发送消息并且它试图显示对话页面时出现以下错误

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

它突出了会话控制器中的这一行编码作为错误点

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

这是对话控制器

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

我主要关注this tutorial设置邮箱gem

我不确定为什么我会收到错误或我可以采取哪些措施来修复错误。

更新

的新错误
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"}

会话/ 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 %>

1 个答案:

答案 0 :(得分:0)

由于Couldn't find Mailboxer::Conversation with 'id'=2的{​​{1}}不存在而导致错误(id = 2)。

所以你可以修改你的错误方法,如下所示

conversions