ルートの「途中」にあるオプションのルート パラメータに制約を設定することは可能ですか?
次のルートを希望します。
get ':city(/:suburb)/:venue_type', venue_type: /bar|restaurant|cafe/
これにより、市内にある特定のタイプの会場のリストが表示されるか、オプションで郊外に絞り込まれます。:venue_types
私がサポートしているのはbar, restaurant
とだけですcafe
。
今、私は次のマッピングを達成したいと思います:
/nyc/manhattan/bar -> :city = nyc, :suburb = manhattan, :venue_type = bar
/nyc/bar -> :city = nyc, :suburb = (nil), :venue_type = bar
/nyc/whatever/cafe -> :city = nyc, :suburb = whatever, :venue_type = cafe
/nyc/whatever -> :city = nyc, :suburb = whatever, :venue_type = (nil) - routing error
これまでのところ、私は仕事をしない次のことを試しました:
class ValidSuburb
INVALID = %w[bar restaurant cafe]
def self.matches?(request)
request.params.has_key?(:suburb) && !INVALID.include?(request.params[:suburb])
end
end
get ':city(/:suburb)/:venue_type', venue_type: /bar|restaurant|cafe/, suburb: ValidSuburb.new
制約によってこれを達成することは可能ですか、それとも複数のルートを持つことに頼る必要がありますか?