Ruby on rails 3 将Rails路径指定为字符串,添加一些参数
给定包含任意路径的字符串:Ruby on rails 3 将Rails路径指定为字符串,添加一些参数,ruby-on-rails-3,path,Ruby On Rails 3,Path,给定包含任意路径的字符串: s = "/api/doctors/123/patients?page=4&active=true" 向该路径添加另一个参数的最佳方法是什么?我正在寻找以下行为: merge_parameters s, :foo => 'bar' # => "/api/doctors/123/patients?page=4&active=true&foo=bar" merge_parameters s, :page => 5 # =>
s = "/api/doctors/123/patients?page=4&active=true"
向该路径添加另一个参数的最佳方法是什么?我正在寻找以下行为:
merge_parameters s, :foo => 'bar'
# => "/api/doctors/123/patients?page=4&active=true&foo=bar"
merge_parameters s, :page => 5
# => "/api/doctors/123/patients?page=5&active=true"
这是否存在
我想做的是在我的API中添加一个
next
链接进行分页,以便客户知道如何获得下一页的结果:
{
"results": [ {...}, {...}, ... ]
"next": "/api/doctors/123/patients?page=5"
}
我希望我可以使用request.path
和这个方法来生成下一页的结果。你可以使用这个类
>rails c
Loading development environment (Rails 3.2.6)
1.9.3p194 :001 > a = URI.parse("/api/doctors/123/patients?page=4&active=true")
=> #<URI::Generic:0x007fa27c33f6f0 URL:/api/doctors/123/patients?page=4&active=true>
1.9.3p194 :002 > a.path
=> "/api/doctors/123/patients"
1.9.3p194 :003 > a.query
=> "page=4&active=true"
rails c
加载开发环境(Rails 3.2.6)
1.9.3p194:001>a=URI.parse(“/api/doctors/123/patients?page=4&active=true”)
=> #
1.9.3p194:002>a.路径
=>“/api/医生/123/患者”
1.9.3p194:003>a.query
=>“页面=4&active=true”
将参数拆分为哈希,将其与新值合并,哈希为字符串,重新赋值,然后uri为字符串或的变体。您可以使用该类进行此操作
>rails c
Loading development environment (Rails 3.2.6)
1.9.3p194 :001 > a = URI.parse("/api/doctors/123/patients?page=4&active=true")
=> #<URI::Generic:0x007fa27c33f6f0 URL:/api/doctors/123/patients?page=4&active=true>
1.9.3p194 :002 > a.path
=> "/api/doctors/123/patients"
1.9.3p194 :003 > a.query
=> "page=4&active=true"
rails c
加载开发环境(Rails 3.2.6)
1.9.3p194:001>a=URI.parse(“/api/doctors/123/patients?page=4&active=true”)
=> #
1.9.3p194:002>a.路径
=>“/api/医生/123/患者”
1.9.3p194:003>a.query
=>“页面=4&active=true”
将参数拆分为散列,将其与新值合并,散列为字符串,重新分配uri为字符串或的变体。如果使用路由帮助程序而不是包含任意路径的字符串:例如,
users\u path
products“/users”
,然后users\u path(:foo=>“bar”,“foo1=>“bar1”)
products“/users?foo1=bar1&foo=bar”
如果使用路由帮助程序而不是包含任意路径的字符串:例如,users\u path
生成“/users”
,则users\u路径(:foo=>“bar”,:foo1=>“bar1”)
生成“/users?foo1=bar1&foo=bar”