I'm using the Mailboxer gem to send messages. Currently, I can see all sent messages, but the inbox does not have any received messages. I've tried so many things, but maybe a fresh set of eyes can spot the problem. I'm still really new to coding, so please let me know if you need more information that what I've posted below. Thanks!
Messages Controller:
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
Conversations Controller:
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
Conversations Index:
<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>
Conversations Partial:
<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>
New Message View:
<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 %>
Aucun commentaire:
Enregistrer un commentaire