1

(コードスクールのオンラインコース、ゼロからのレールアプリパート1の演習を行っています)

これが私のroutes.rb

resources :destinations, only: :index 

resources :trips do
  resources :destinations
end

そのrake routes結果、次の 2 つのルートが同じアクションに移動します。

           Prefix Verb   URI Pattern                             Controller#Action
     destinations GET    /destinations(.:format)                 destinations#index
trip_destinations GET    /trips/:trip_id/destinations(.:format)  destinations#index

に一致させる、つまりにリダイレクトしたいのtrip_destinationsですtrip#show/trips/:id、ハードコードするroutes.rbか、リダイレクトを追加するなどの解決策がありdestination#indexます。どちらの方がよいですか?このタスクを達成するためのベスト プラクティスはありますか?

4

1 に答える 1

1
resources :trips do
  resources :destinations do
    member do
      get 'index' => 'trips#show'
    end
  end
end

これはうまくいきますが、応答が要求と一致しないため、設計の誤りに基づいています。つまり、悪路です。旅行の目的地インデックス リソースは、旅行オブジェクト自体ではなく、旅行の目的地インデックスで応答する必要があります。

パスは/trips/:id/destinationsリダイレクトまたはレンダリングであってはなりません/trips/:idtrips/:id/destinationsアプリケーションが適切なリソースで応答できない限り、そもそも を 指すリンクがあってはなりません。

派手なルートや URL 表示に夢中になるのはとても簡単です。ルートが理にかなっていることを確認してください。将来のトラブルを大幅に軽減します。

于 2013-05-17T07:43:11.020 に答える