Rails routes matching query parameters

别来无恙 提交于 2019-12-01 04:05:59

The following solution is based on the "Advanced Constraints" section of the "Rails Routing from the Outside In" rails guide (http://guides.rubyonrails.org/routing.html).

In your config/routes.rb file, include a recognizer class have a matches? method, e.g.:

class FruitRecognizer
  def initialize(fruit_type)
    @fruit_type = fruit_type.to_sym
  end

  def matches?(request)
    request.params.has_key?(@fruit_type)
  end
end

Then use objects from the class as routing constraints, as in:

map.connect "api/my/path", :contraints => FruitRecognizer.new(:apple), :controller => 'apples_controller', :action => 'my_action'

Unless there is a concrete reason why you can't change this, why not just make it restful?

map.connect "api/my/path/bananas/:id, :controller => "bananas_controller", :action => "my_action"

If you have many parameters, why not use a POST or a PUT so that your parameters don't need to be exposed by the url?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!