Rails 4中的嵌套路由和url帮助器

时间:2014-01-19 16:57:21

标签: ruby-on-rails ruby ruby-on-rails-4

我有嵌套路线,如下所示:

resources :venues do
    #Halls
    get "hall/:id/exhibition" => "halls#exhibition", as: :exhibition
    get "hall/:id/visit" => "halls#visit", as: :hall_visit
    get "venue_structure", :to => "venues#venue_structure"
    resources :asset_types, :booths_tags, :tags, :uploaded_files, :events, :chats
    resources :halls do
        resources :webcasts
        resources :booths do
            resources :chats
        end
    end
end

这种方法的问题在于我必须在url helper中加入三个参数,用于嵌套的,如下所示:

venue_hall_booth_path(@booth.hall.venue, @booth.hall, @booth)

除了我每次使用这个帮助器时必须将三个不同的资源作为参数放入时,还有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用shallow routes

resources :halls, shallow: true do
  resources :webcasts
  resources :booths do
    resources :chats
  end
end

这使您无需使用父级即可访问成员网址。除非是newcreate行为。

或者您可以单独定义它们。

resources :booths do
  resources :chats
end
resources :halls do
  resources :webcasts
  resources :booths
end