让我们想象一下,我有一个如下定义的路线:
constraints MyRouteConstraint.new do get ':param/:param1/:param2',to: 'controller#action' end
如何在控制器中获得’:param /:param1 /:param2’模式?
我知道有Rails.application.routes.recognize_path“/ param / param1 / param2”但它引发了一个错误’No Route matches’
更新:
Rails.application.routes.router.recognize(request) do |route,matches,parameters| return route.path.spec.to_s if route.path.required_names.all? { |p| request.params.keys.include? p } end.flatten.compact.first
解决方法
不太确定这是否是您正在寻找的,这意味着在您的视图中使用.
您可以尝试request.fullpath
返回String完整路径,包括请求的最后一个URL的params.
# get "/articles" request.fullpath # => "/articles" # get "/articles?page=2" request.fullpath # => "/articles?page=2"
请参阅此处的文档:
http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url