【问题标题】:How to set up partially nested resourceful routes?如何设置部分嵌套的资源路由?
【发布时间】:2011-11-15 23:22:36
【问题描述】:

在我的 Rails 网站上,用户发布了Listings 他们想要出售的商品。其他用户可以浏览这些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 的上下文之外创建,所以我嵌套了它们资源丰富的路由。但是,它们不需要在创建后查看/编辑 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?

【问题讨论】:

    标签: ruby-on-rails-3 routes url-routing nested-resources


    【解决方案1】:

    这应该可行:

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

    你总是可以在命令行中运行它

    rake routes | grep message_thread
    

    新链接将是

    new_listing_message_threads_path(@listing)
    

    【讨论】:

      猜你喜欢
      • 2016-07-19
      • 2014-12-09
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多