Question

I'm using mailboxer gem, I can send and receive message to and from the same user( self) dexter can send messages to dexter, but when I login as dexter2 and send messages to dexter I get undefined method error, when i click the back button and refresh the conversation, the messages is there so the message is being sent but I keep getting the

undefined method 'mailboxer_email" for #<User:0x007f6ed0907040>

messages_controller:

class MessagesController < ApplicationController


 def new
   @user = User.find_by_username(params[:user])
@message = current_user.messages.new

end

# POST /message/create
def create
@recipient = User.find_by_username(params[:user])
current_user.send_message(@recipient, params[:body], params[:subject])
flash[:notice] = "Message has been sent!"
redirect_to :conversations
end

end

conversations/show.html.erb

<%= conversation.subject %>

A conversation with
<% conversation.participants.each do |participant| %>
 <% if participant != current_user %>
  <%= participant.username%>
 <% end %>
<% end %>
<%= content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt| %>
 <% message = receipt.message %>
 <%= message.sender.username %>

 <%= simple_format h message.body %>

 Sent <%= conversation.updated_at.strftime("%a, %m/%e/%Y %I:%M %p") %>

<% end %>

<%= render 'messages/form', conversation: conversation %>

messages/form

  Reply:
 <%= form_for :message, url: [:reply, conversation] do |f| %>
 <%= f.text_area :body %>
 <%= f.submit "Send Message", class: 'btn btn-primary' %>
 <%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
 <% end %>

messages/new.html.erb

Send a message to

<%= @user.username %>
<%= form_tag({controller: "messages", action: "create"}, method: :post) do %>
<%= label_tag :subject %>
<%= text_field_tag :subject %>
<%= label :body, "Message text" %>
<%= text_area_tag :body %>
<%= hidden_field_tag(:user, "#{@user.username}") %>
 <%= submit_tag 'Send message' %>
<% end %>    
Was it helpful?

Solution

For what I read in the documentation you should define this method inside your model:

def mailboxer_email(object)
 #return the model's email here
end

This will ensure that your model has what mailboxer needs to identify the resource, you can check the documentation here

Initializer (config/initializers/mailboxer.rb):

Mailboxer.setup do |config|
  # ...
  #Configures the methods needed by mailboxer
  config.email_method = :email
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top