简体   繁体   中英

Nested routes and url helper in Rails 4

I have nested routes which goes something like this:

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

Problem with this approach is that I have to put in three paramters in url helpers for nested ones like below:

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

Is there a better approach to doing this other than me having to put in three different resources as parameters each time when I use this helper?

You can use shallow routes :

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

This allows you access member urls without having to use the parent. Unless it's a new or create actions.

Or you can define them separately.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM