winston
winston

Reputation: 3100

Unable to add custom routes in rails

I am using the mailboxer gem in my rails app to handle private messages between users. I have a separate page to view your inbox and a separate page to view your trash folder. The URL localhost/conversations succesfully loaded the inbox. I want to have a link that can load a separate page for trash, such as localhost/conversations/trashbin. However, I can't get Rails to recognize any routes I create for such a page.

In addition, going directly to that URL displays and error:

Couldn't find Conversation with id=trashbin

I understand why the error is occurring but I don't know how to fix it. The issue is with my routes file:

resources :conversations do
    member do
      post :reply
      post :trash
      post :untrash
      get 'trashbin', :action => 'trashbin'
    end
  end

Using resources is causing the app to look for a specific conversation. This is useful throughout the rest of the application except for this one case. I simply want to collect all of the messages that were marked as trash. How can I edit this routes file to accomplish this?

Here are the links I have on the index conversations page:

<a href="<%= conversations_path %>"> <%= @conversations.count %> Inbox </a> | <a href="<% trashbin_conversations_path %>"> <%= @trash.count %> Trash </a> 

Thanks!

EDIT:

Thanks to the answers below I have updated the routes to:

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

However, the URL conversations/trashbin now displays an Unknown action error: The action 'trashbin' could not be found for ConversationsController

My ConversationsController clearly has the action defined. Why is this error appearing?

Upvotes: 0

Views: 95

Answers (2)

Marjan
Marjan

Reputation: 1418

Put the route in a collection scope

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

Upvotes: 2

PinnyM
PinnyM

Reputation: 35533

Don't use a member route - use a collection route instead:

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

See here for more info on this.

Upvotes: 3

Related Questions