票有可以发送消息的用户 - rails 消息传递 [英] ticket has users that can send messages - rails messaging
问题描述
我不确定应该如何为私人消息构建路由.当用户单击票证时,它会显示票证详细信息以及向用户发送关于票证的消息的选项.然而,一个新的消息路由看起来像 users/:username/messages/new
,但它应该反映用户(发送者)正在向另一个用户(接收者)发送关于当前票证的消息正在查看.如何使我的路由 RESTful?
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?
可以在此处找到模型:receiver_id 不保存 - rails 消息传递
编辑
我最终将 routes.rb 更改为:
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.
... 所以在你的 routes.rb 文件...
... so in your routes.rb file ...
resources :tickets do
resources :recipients do
resources :messages
end
end
这会给你一个/tickets/[ticket-id]/recipients/[recipient-id]/messages/new
的路径.请注意,id 不必是记录的数字 id.您的控制器可以决定如何从参数中查找记录,您可以在模型上定义 #to_param
以控制路径辅助方法将使用的内容.
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_param
on your models to control what the path helper methods will use.
当路由到与该模式匹配的路径的请求时,它将调用 MessagesController
的 #new
方法并接收 #params中的值代码>:ticket_id 和 :recipient_id.
When a request to a path matching that pattern is routed, it will invoke the #new
method of MessagesController
and will receive values in #params
for :ticket_id and :recipient_id.
这篇关于票有可以发送消息的用户 - rails 消息传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!