0

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
Community
  • 1
  • 1
winston
  • 3,000
  • 11
  • 44
  • 75

1 Answers1

1

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.

toobulkeh
  • 1,618
  • 1
  • 14
  • 22
  • Thanks for the reply! I forgot to post the button code that is on the user profile page. I have something similar to what you posted: `<%= button_to "Send a message", new_message_path(:user => @user) %>` – winston Dec 09 '13 at 02:37
  • I'm starting to think the issue is with the Submit button on the messages/new page. When I click the Submit button, and the error appears, the URL in the URL bar in my browser is `http://localhost:3000/messages` . Shouldn't it go to /messages/create? – winston Dec 09 '13 at 02:42
  • 1
    Hey @james, I've updated my answer to hopefully correct a few things you were trying to do. Let me know if you still need help – toobulkeh Dec 09 '13 at 04:31
  • Thanks for the update! I have to run to work but I was able to try a few things. I had to change the routes due to an error- it was missing the to: keyword. The new_message_for_user method didn't exist so I stuck with the existing new_message_path. Routes line in question is `post 'new/:user', to: 'messages#create`. When I click on the button on the User page the URL looks like `http://localhost:3000/messages/new.3` with the Active Record error `Couldn't find User without an ID`. I'll play with it more tonight when I return from work. Thanks again! – winston Dec 09 '13 at 12:20