のようなパスセグメントの代わりにパラメーターを使用します/url/posts?datefrom=2012-12-05&dateto=2012-12-31
。これは、ルートに影響を与えず、RESTfulであるためです。
次に、次のクラスマクロが必要です。
class PostsController < ApplicationController
allows_query_by_daterange
#...
end
そしてミックスイン:
module QueryByDateRange
def allows_query_by_daterange
include QueryByDateRange::Filter
end
module Filter
extend ActiveSupport::Concern
included do
before_filter :apply_daterange # maybe only on the index action?
end
def apply_daterange
# set @datefrom and @dateto from params
end
end
end
すべてのコントローラーで使用できるようにします。
class ApplicationController
extend QueryByDateRange
end
アクションでは、少なくともインスタンス変数が設定されています。このソリューションはさらに推進される可能性があり、条件がARelステートメントに自動的に追加され、マクロを追加するだけで済みます。
うまくいけば、私の答えはあなたに行くべき可能な方向を示すことができます。