link_to传递查询参数

时间:2013-10-09 15:48:47

标签: ruby-on-rails-4 activeadmin link-to

需要能够生成以下URL字符串

http://localhost:3000/admin/cities?q%5Bprovince_id_eq%5D=1&commit=Filter&order=city_name_asc

如何设置此link_to?

link_to(p.cities.count, admin_cities_path)

2 个答案:

答案 0 :(得分:2)

您可以将查询参数作为哈希传递给URL帮助程序,例如在我的控制台中运行以下命令,我得到以下哈希:

url = "http://localhost:3000/admin/cities?q%5Bprovince_id_eq%5D=1&commit=Filter&order=city_name_asc"    
query = URI.parse(url).query
hash = Rack::Utils.parse_nested_query(query)
#=> { "q" => { "province_id_eq" => "1" }, "commit" => "Filter", "order" => "city_name_asc" }

然后你就做了

admin_cities_url(hash)

返回原始网址。

答案 1 :(得分:1)

可能this会帮助你,看看“link_to还可以生成锚点或查询字符串的链接”

link_to(p.cities.count, admin_cities_path(q: { province_id_eq: 1 }, order: "city_name_asc"))