Search code examples
ruby-on-railsmailboxer

undefined method `reply_mailboxer_conversation_path'


When I attempt to render this view:

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 %>

I get the error: undefined method 'reply_mailboxer_conversation_path'

Routes:

 resources :messages do
  member do
      post :new
  end
end
resources :conversations do
  member do
      post :reply
      post :trash
      post :untrash
  end
  collection do
      get :trashbin
      post :empty_trash
  end
end

rake routes output:

         user_session POST   /users/sign_in(.:format)             devise/sessions#create
 destroy_user_session DELETE /users/sign_out(.:format)            devise/sessions#destroy
        user_password POST   /users/password(.:format)            devise/passwords#create
    new_user_password GET    /users/password/new(.:format)        devise/passwords#new
   edit_user_password GET    /users/password/edit(.:format)       devise/passwords#edit
                      PATCH  /users/password(.:format)            devise/passwords#update
                      PUT    /users/password(.:format)            devise/passwords#update
 cancel_user_registration GET    /users/cancel(.:format)              registrations#cancel
    user_registration POST   /users(.:format)                     registrations#create
 new_user_registration GET    /users/sign_up(.:format)             registrations#new
 edit_user_registration GET    /users/edit(.:format)                registrations#edit
                      PATCH  /users(.:format)                     registrations#update
                      PUT    /users(.:format)                     registrations#update
                      DELETE /users(.:format)                     registrations#destroy
                users GET    /users(.:format)                     users#index
                      POST   /users(.:format)                     users#create
             new_user GET    /users/new(.:format)                 users#new
            edit_user GET    /users/:id/edit(.:format)            users#edit
                 user GET    /users/:id(.:format)                 users#show
                      PATCH  /users/:id(.:format)                 users#update
                      PUT    /users/:id(.:format)                 users#update
                      DELETE /users/:id(.:format)                 users#destroy
                 root GET    /                                    profiles#index
              message POST   /messages/:id(.:format)              messages#new
             messages GET    /messages(.:format)                  messages#index
                      POST   /messages(.:format)                  messages#create
          new_message GET    /messages/new(.:format)              messages#new
         edit_message GET    /messages/:id/edit(.:format)         messages#edit
                      GET    /messages/:id(.:format)              messages#show
                      PATCH  /messages/:id(.:format)              messages#update
                      PUT    /messages/:id(.:format)              messages#update
                      DELETE /messages/:id(.:format)              messages#destroy
   reply_conversation POST   /conversations/:id/reply(.:format)   conversations#reply
   trash_conversation POST   /conversations/:id/trash(.:format)   conversations#trash
 untrash_conversation POST   /conversations/:id/untrash(.:format) conversations#untrash
trashbin_conversations GET    /conversations/trashbin(.:format)    conversations#trashbin
empty_trash_conversations POST   /conversations/empty_trash(.:format) conversations#empty_trash
        conversations GET    /conversations(.:format)             conversations#index
                      POST   /conversations(.:format)             conversations#create
     new_conversation GET    /conversations/new(.:format)         conversations#new
    edit_conversation GET    /conversations/:id/edit(.:format)    conversations#edit
         conversation GET    /conversations/:id(.:format)         conversations#show
                      PATCH  /conversations/:id(.:format)         conversations#update
                      PUT    /conversations/:id(.:format)         conversations#update
                      DELETE /conversations/:id(.:format)         conversations#destroy

Really not sure what I'm doing wrong. If I've left out any important code, please just let me know.

https://github.com/portOdin/gfi2/tree/june6/app/views


Solution

  • The error you're receiving is because of how you're defining your form_for block:

    <%= form_for :message, url: [:reply, conversation] do |f| %>
    

    From the docs:

    Resource-oriented style

    In the examples just shown, although not indicated explicitly, we still need to use the :url option in order to specify where the form is going to be sent. However, further simplification is possible if the record passed to form_for is a resource, i.e. it corresponds to a set of RESTful routes, e.g. defined using the resources method in config/routes.rb. In this case Rails will simply infer the appropriate URL from the record itself. For example,

    <%= form_for @post do |f| %>
      ...
    <% end %>
    

    is then equivalent to something like:

    <%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
      ...
    <% end %>
    

    --

    Nested

    When you use an array for the resource part of form_for, you're basically telling rails that conversation will inherit from reply.

    We use it like this:

    <%= form_for [:admin, @object] do |f| %>
    

    This basically treats the url attribute like this:

    url: reply_conversation_path

    I don't know where it got mailboxer from, but either way, the problem will be caused by the declaration of [:reply, conversation].

    --

    Fix

    There are two ways to fix this.

    The first is to use the url: attribute inside the form_for, like this:

    <%= form_for [:reply, @conversation], url: reply_conversation_path(conversation) do |f| %>
    

    The second is to use the correct formatting:

    <%= form_for @reply, url: reply_conversation_path(@conversation) do |f| %>
       ...
    <% end %>