5

次のようなルートを設定しようとしています: 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ます。

4

4 に答える 4

1

より多くの RESTful resources :posts(config/routes.rb 内) を使用して、クエリ文字列でパラメーターを送信できます。

このアプローチでは、すべてのパラメーターがオプションであり、事前定義されたパラメーターの使用に制限されません。

于 2012-10-25T19:51:05.537 に答える
0

次のようなことを試すことができます:

match '/filter/*category_or_status' => 'posts#index', as: 'filter'

これにより、独自のフィルター パスを構築できます。次に、コントローラーで解析params[:category_or_status]し、カテゴリまたはステータスが指定されている場合はそれらを取得できます。

于 2012-10-26T11:01:59.663 に答える
0

これはうまくいきますか?

resources :posts do
  collection do
    match '/:category(/:status)', to: 'posts#index', as: 'filter'
    match '/:status', to: 'posts#index', as: 'filter'
  end
end

少なくともそれが役立つことを願っています!

于 2012-10-25T19:57:55.403 に答える