简体   繁体   English

Rails:在url中查询params

[英]Rails: query params in the url

Is it possible to get routes like the following: 是否可以获得如下路线:

http://example.org/test/url/p/some-123/random-444/keys-245 

and this url should be resolved to params: 这个网址应该解决为params:

{
  controller:"home",
  action:"get",
  mod:"test",
  submob:"url",
  some:"123",
  random:"444",
  keys:"245"
}

Sure - you'll have to define it all in your routes : 当然 - 您必须在路线中定义所有内容:

#config/routes.rb
resources :controller do
    get ":mod(/:submod/:some/:keys)", to "controller#action"
end

Can we use this 我们可以用它吗?

  match '/:mod/:submod/:some/:keys' => "controller#action", :as => test_profile

and we use it like 我们就像使用它一样

 link_to "Own path", test_profile_path(mod: "test", submod: "url", some: "123", keys: "245")

get ':mod/:submod/p/*extra_params', to: 'home#index'

Then in your HomeController 然后在你的HomeController

def params
  orig_params = super
  extra_params = orig_params.delete(:extra_params)
  extra_params = Hash[extra_params.split('/').map { |p| p.split('-') }]
  orig_params.merge(extra_params)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM