如何设置部分嵌套的资源路由?
在我的 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
上下文。只有 new
和 create
才需要它。行动。我是这样设置的:
resources :message_threads, :only => [:show, :destroy]
resources :listings do
resources :message_threads, :only => [:new, :create]
end
我似乎无法弄清楚 MessageThreadsController
的 new
操作视图中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效:
您始终可以在命令行中运行它
新链接将是
This should work:
You can always run this in the command line
The new link would be