I’m unsure how the routes should be constructed for private messages. When a user clicks on a ticket, it reveals ticket details and an option to message the user regarding the ticket. However, a new message route looks like users/:username/messages/new, but it should reflect that the user (sender) is sending a message to another user (receiver) regarding the ticket that is currently being viewed. How can I make my routes RESTful?
Models can be found here: receiver_id not saving – rails messaging
EDIT
I ended up changing my routes.rb to:
resources :users do
resources :tickets do
resources :messages
end
end
resources :tickets
Is there any disadvantage to having multiple resources defined?
Generally, the user sending the message does not need to be represented in the path, since that user is identified via the current user session, so that leaves the recipient, the ticket, and the action of creating a new message that must be identified by the path.
Those remaining things can be represented by nested collections, and the order of nesting is somewhat arbitrary, except that the messages collection should be the most deeply nested since that’s what will receive the “new” action. The options for nesting, then are tickets, recipients, messages or recipients, tickets, messages. To me, the first nesting seems more intuitive.
… so in your routes.rb file …
That will give you a path of
/tickets/[ticket-id]/recipients/[recipient-id]/messages/new. Note that the ids do not need to be the numeric ids of the records. Your controller can decide how to find a record from the parameter, and you can define#to_paramon your models to control what the path helper methods will use.When a request to a path matching that pattern is routed, it will invoke the
#newmethod ofMessagesControllerand will receive values in#paramsfor :ticket_id and :recipient_id.