Rails 3 - 嵌套资源和会员/收藏

发布于 2024-12-03 19:57:19 字数 907 浏览 2 评论 0原文

有没有办法避免这样做...

resources :parents do
  resources :children do
    collection do
      get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
    end
  end
end

resources :children do
  collection do
    get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
  end
end

我认为可以这样做...

resources :parents do
  resources :children do
end

resources :children do
  collection do
    get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
  end
end

原因是我希望能够使用这两条路线...

/children/search/term/0/10
/parents/1/children/search/term/0/10

Is there a way to avoid having to do this...

resources :parents do
  resources :children do
    collection do
      get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
    end
  end
end

resources :children do
  collection do
    get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
  end
end

I thought it would be possible to just do this...

resources :parents do
  resources :children do
end

resources :children do
  collection do
    get "/search/:term/:offset/:limit.:format", :action => "search", :constraints => { :term => /\w+/, :offset => /\d+/, :limit => /\d+/ }
  end
end

The reason being is I want to be able to use both of these routes...

/children/search/term/0/10
/parents/1/children/search/term/0/10

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

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

发布评论

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

评论(1

迷乱花海 2024-12-10 19:57:19

这似乎可以

def define_children 
  resources :children do
    collection do
      get :search
    end
  end
end

define_children
resources :parents do
  define_children
end

如果使用了通过 parent 的路由, 在 params 中设置 :parent_id 。否则它不会出现。为了清楚起见,我省略了约束。另外,您可能应该将 .format 设为可选。

This seems to do the trick

def define_children 
  resources :children do
    collection do
      get :search
    end
  end
end

define_children
resources :parents do
  define_children
end

:parent_id will be set in params if the route through parent has been used. Otherwise it won't be present. I have omitted constraints for clarity. Also you probably should make the .format optional.

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