对话页面上的 Mailboxer gem 活动记录错误
Mailboxer gem active record error on the conversations page
我正在尝试设置邮箱 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"}
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 %>
你得到的错误(Couldn't find Mailboxer::Conversation with 'id'=2
)是因为 conversions
的 id = 2
不存在。
所以你可以像下面这样修改你的错误方法
def get_conversation
@conversation = @mailbox.conversions.new || @mailbox.conversations.find(params[:id])
end
我正在尝试设置邮箱 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"}
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 %>
你得到的错误(Couldn't find Mailboxer::Conversation with 'id'=2
)是因为 conversions
的 id = 2
不存在。
所以你可以像下面这样修改你的错误方法
def get_conversation
@conversation = @mailbox.conversions.new || @mailbox.conversations.find(params[:id])
end