私はこれについて考え、いくつかの結論に達しました。
- 理想的には HTTP 404 ステータス コードを返したいパスがいくつかあるため、すべてのルートで単一ページ アプリの HTML を返したくない場合があります。
- HTTP ルートは JSON ルートとは異なります
- さまざまなフォーマットをさまざまなコントローラーにルーティングする必要があります
- Rails ルーターですべての HTML ルートを定義すると、それを使用して JavaScript ルーターを生成できるようになると有利な場合があります。これを行うgemが少なくとも1つあります
- Rails にはこの機能がなく、この gem https://github.com/svenfuchs/routing-filterは適切なツールとは思えません。ここに私の試みがあります: Rails routing-filter すべての html リクエストを 1 つのアクションにルーティングします
- モジュール Api の下で JSON API の名前空間を作成して、ルーティングの競合を回避する必要があることは悪いことではありません。
- 単一ページのアプリで Google にコンテンツを表示しないでください。コンテンツが重複しているため、アクセスを禁止されます。
私は少し異なるアプローチを取りましたが、これは実際には質問に答えていませんが、いくつかの利点があると思います:
これが私のconfig/routes.rbです
FooApp::Application.routes.draw do
# Route all resources to your frontend controller. If you put this
# in a namespace, it will expect to find another frontend controller
# defined in that namespace, particularly useful if, for instance,
# that namespace is called Admin and you want a separate single page
# app for an admin interface. Also, you set a convention on the names
# of your HTML controllers. Use Rails's action_missing method to
# delegate all possible actions on your frontend controllers.
def html_resources(name, options, &block)
resources(name, options.merge(:controller => "frontend"), &block)
end
# JSON API
namespace :api do
resources :things, :except => [:edit, :new]
end
# HTML
html_resources :things, :only => [:edit, :new, :index, :show] do
get :other_action
end
end
class FrontendController < ApplicationController
def action_missing(*args, &block)
render :index
end
end