次のようなルートを設定しようとしています: acme.com/posts/:category/:status
. :category
とはどちら:status
もオプションです。私は多くのバリエーションを書きましたが、どれもうまくいきませんでした:
resources :posts do
match '(/:category)(/:status)', to: 'posts#index', as: 'filter', on: :collection
end
# Category Links
link_to "Questions", filter_posts_path(:questions)
link_to "Suggestions", filter_posts_path(:suggestions)
# Status Links
link_to "Published", filter_posts_path(params[:category], :published)
link_to "Draft", filter_posts_path(params[:category], :draft)
アイデアは、1) カテゴリによるフィルタリング、2) ステータスによるフィルタリング、および 3) 両方が利用可能な場合、カテゴリとステータスの両方によるフィルタリングを可能にすることです。現在のセットアップも私の/posts/new
パスを壊し、常ににリダイレクトしposts#index
ます。