Rails 路线助手 - 一个模型可以填充许多参数
我有以下路线:
get "/:user_name/things/:thing_name" => "things#show", :as => "show_user_thing"
Thing
属于user
。因此,只要有一个 thing
实例,我就有了两个参数。但是,当使用路线助手时,我被迫单独指定每个段,如下所示:
show_user_thing_path(@thing.user, @thing)
这很糟糕。我更愿意这样做:
show_user_thing_path(@thing)
但是我该如何通过“路由助手方式”来做到这一点呢?我很乐意仍然使用所有像这样的路线的铁路好东西。有什么想法吗?
I have the following route:
get "/:user_name/things/:thing_name" => "things#show", :as => "show_user_thing"
Thing
belongs to user
. So with just an instance of thing
I have both parameters. However, when using the route helpers, I'm forced to specify each segment separately like so:
show_user_thing_path(@thing.user, @thing)
This sucks. I'd much rather do just this:
show_user_thing_path(@thing)
But how do I do this the 'route helper way'? I'd love to still use all the rails goodies for route's like these. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我感受到你的痛苦。在我经常使用 url 帮助器的情况下,我只编写自己的帮助器。
当然,您必须稍微修改它以包含任何选项和格式,但我认为您明白我在说什么。
I feel your pain. In the cases where I uses the url helper a lot, I just write my own helper.
Of course you'd have to modify it slightly to include any options and formatting, but I think you get the idea of what I'm saying.