Implementing Mailboxer gem in Rails app. ActiveRecord::RecordNotFound

836 views Asked by At

I'm trying to implement the fantastic Mailboxer gem into my Rails app. I want to include a button on a User profile page that loads a form to send that user a private message. I tried to use the code posted in this question as a guide without any luck: Rails mailboxer gem, send message to user using form .

Clicking on the button on the User page will load the private message form. However, clicking on the Send button on that form displays the following error:

ActiveRecord::RecordNotFound in MessagesController#create Couldn't find User without an ID

Here's the relevant code. I'll post my thoughts below the code samples:

messages controller

class MessagesController < ApplicationController
  def index
  end

# GET /message/new
  def new
  @user = User.find(params[:user])
  @message = current_user.messages.new
    # display form
  end

  # POST /message/create
  def create
    @user = User.find(params[:user])
    @message = current_user.connections.build(params[:message])
    #current_user.send_message(@user, params[:body], params[:subject])
  end
end

new.html.erb (Messages view - new)

<%= form_for(@message) do |f| %>
            <div>Subject
            <%= f.text_field :subject %></div>
            <div>Message
            <%= f.text_area :body%></div>
             <%= f.hidden_field(:user, :value => @user.id) %>
            <p><%= f.submit 'Send message'%></p>
        <% end %>

The Mailboxer documentation recommends using current_user.send_message(@user, params[:body], params[:subject]) (which I commented out) in the create function but then I'm unable to bring over the User that I obtained with a GET in the "new" function. I'm trying to use the new function to send it to the create function but I'm completely stuck. Why can't my current create function not find the User ID from the new function?

For reference, here is a link to the gem documentation: https://github.com/ging/mailboxer

Thanks!

EDIT: Button code on user profile page

<%= button_to "Send a message", new_message_path(:user => @user) %>

Relevant routes

resources :messages do
    member do
      post :reply
      post :trash
      post :untrash
      post :new
    end
1

There are 1 answers

4
toobulkeh On

You'll need to link the email button to new_message_path(@user.id) on the User's profile page.

It potentially looks like the @user in /message/new isn't being set from the DB because it doesn't have a params[:user] variable to properly index off of.

Your route should look something like:

resources :messages do
  collection do
    post 'new/:user', 'messages#create', as 'new_message_for_user'
  end
  member do
    ..
  end
end

and then your button would look like:

= button_to 'Send a message', new_message_for_user(@user), method: :post

I'm sure some of my preferences are showing through, but there's some Rails-Way that it feels like your skipping. Also, if you've overridden your User object, you might have to change the button.