我正在使用Mailboxer gem发送消息。目前,我可以看到所有已发送的消息,但收件箱中没有收到任何消息。我尝试了很多东西,但也许一双新鲜的眼睛可以发现问题。我对编码仍然很陌生,所以如果您需要更多关于我在下面发布的信息,请告诉我。谢谢!
消息控制器:
class MessagesController < ApplicationController
before_action :authenticate_user!
def new
@user = User.find_by(id: params[:to].to_i) if params[:to]
@chosen_recipient = User.find_by(id: params[:to].to_i) if params[:to]
end
def create
recipient = User.find(id: params[:to].to_i) if params[:to]
current_user.send_message(recipient, params[:message][:body], params[:message])
flash[:success] = "Message has been sent!"
redirect_to conversations_path
end
private
def set_message
@recipient = User.find(params[:receiver_id])
end
def message_params
params.require(:message).permit(:body)
end
end
对话控制器:
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :get_mailbox
before_action :get_conversation, except: [:index, :empty_trash]
before_action :get_box, only: [:index]
def index
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
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_conversation
@conversation ||= @mailbox.conversations.find(params[:id])
end
def get_mailbox
@mailbox ||= current_user.mailbox
end
end
对话指数:
<h1>Your Conversations</h1>
<div class="row">
<div class="col-sm-3">
<ul class="nav nav-pills nav-stacked">
<%= mailbox_section 'inbox', @box %>
<%= mailbox_section 'sent', @box %>
<%= mailbox_section 'trash', @box %>
</ul>
</div>
<div class="col-sm-9">
<% if @box == 'inbox' %>
<%= current_user.mailbox.inbox %>
<% end %>
<% if @box == 'trash' %>
<p><%= link_to 'Empty trash', empty_trash_conversations_path, class: 'btn btn-danger', method: :delete,
data: {confirm: 'Are you sure?'} %></p>
<% end %>
<ul class="list-group">
<%= render partial: 'conversations/conversation', collection: @conversations %>
</ul>
<%= will_paginate %>
</div>
</div>
对话部分:
<li class="list-group-item clearfix">
<div class="btn-group-vertical pull-right">
<% if conversation.is_trashed?(current_user) %>
<%= link_to 'Restore', restore_conversation_path(conversation), class: 'btn btn-xs btn-info', method: :post %>
<% else %>
<%= link_to 'Move to trash', conversation_path(conversation), class: 'btn btn-xs btn-danger', method: :delete,
data: {confirm: 'Are you sure?'} %>
<% end %>
</div>
<p><%= render 'conversations/participants', conversation: conversation %></p>
<p><%= link_to conversation.last_message.body, conversation_path(conversation) %>
<small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %> </span>)</small></p>
</li>
新邮件视图:
<h1>Start a conversation</h1>
<%= form_tag messages_path, method: :post do %>
<div class="form-group">
<%= label_tag 'message[body]', 'Message' %>
<%= text_area_tag 'message[body]', nil, cols: 3, class: 'form-control', required: true %>
</div>
<%= submit_tag 'Send', class: 'btn btn-primary' %>
<% end %>
用户模型:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
acts_as_messageable
has_many :languages_users
has_many :languages, :through => :languages_users
def mailboxer_email(object)
email
end
end