开发者

Rails 3 - Nested Resources & Members/Collections

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜