簡體   English   中英

Rails 4中的嵌套路由和URL幫助器

[英]Nested routes and url helper in 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幫助器中為嵌套的參數添加三個參數,如下所示:

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

除了每次使用該幫助程序時都必須放入三個不同的資源作為參數之外,還有其他更好的方法嗎?

您可以使用淺層路線

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

這樣,您無需使用父級即可訪問成員url。 除非是new動作或create動作。

或者,您可以分別定義它們。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM