如何设置部分嵌套的资源路由?

发布于 2024-12-15 15:01:29 字数 919 浏览 4 评论 0原文

在我的 Rails 网站上,用户发布他们想要出售的商品的 Listing。其他用户可以浏览这些列表并单击链接联系卖家,这将创建一个 MessageThread

# MessageThread properties...
listing_id
poster_id # (listing_id's owner.. this is a shortcut for to avoid joins on the database)
sender_id
body # (text of message)

MessageThreads 绝不会在 Listing 的上下文之外创建code>,所以我嵌套了他们足智多谋的路线。但是,它们在创建后不需要查看/编辑 Listing 上下文。只有 newcreate 才需要它。行动。我是这样设置的:

resources :message_threads, :only => [:show, :destroy]

resources :listings do
   resources :message_threads, :only => [:new, :create]
end

我似乎无法弄清楚 MessageThreadsControllernew 操作视图中的 form_for 语法...我做了 form_for @message_thread 但这不起作用。这是设置该对象的正确方法吗?我应该避免使用资源丰富的路由,还是可以将列表 ID 作为 URL 中的参数传递,而不是嵌套路由?或者总是引用列表下方的message_thread?

On my Rails site, users post Listings of items they want to sell. Other users can browse these listings and click a link to contact the seller, which should create a MessageThread:

# MessageThread properties...
listing_id
poster_id # (listing_id's owner.. this is a shortcut for to avoid joins on the database)
sender_id
body # (text of message)

MessageThreads are never created outside the context of a Listing, so I nested their resourceful routes. However, they don't need the Listing context to be viewed/edited after they are created.. it's only necessary for the new and create actions. I set it up like this:

resources :message_threads, :only => [:show, :destroy]

resources :listings do
   resources :message_threads, :only => [:new, :create]
end

I can't seem to figure out the form_for syntax in the new action view for the MessageThreadsController... I do form_for @message_thread but that isn't working. Is this the right way to set this object up? Should I avoid using resourceful routes, or maybe pass the listing ID as a parameter in the URL, instead of nesting the routes? Or always reference the message_thread underneath a listing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-12-22 15:01:29

这应该有效:

form_for @message_thread, :url => listing_message_threads_path(@listing)

您始终可以在命令行中运行它

rake routes | grep message_thread

新链接将是

new_listing_message_threads_path(@listing)

This should work:

form_for @message_thread, :url => listing_message_threads_path(@listing)

You can always run this in the command line

rake routes | grep message_thread

The new link would be

new_listing_message_threads_path(@listing)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文