简体   繁体   English

Ruby on Rails将URL匹配到控制器动作

[英]ruby on rails Match a url to a controller action

I need to match this link /links/128/dev?usrA=mike&usrB=carl with my controller. 我需要将此链接/links/128/dev?usrA=mike&usrB=carl与我的控制器进行匹配。

I tried using: 我尝试使用:

match 'links/:id/dev?usrA=:curr&usrB=:prev', to: 'links#index', via: :get

But it does not work. 但这行不通。

Is there any way to match this URL with my controller? 有什么办法可以将该URL与我的控制器进行匹配?

I generally avoid match and prefer the more explicit get (if you will only allow a GET ) 我通常避免match而更喜欢使用更明确的get (如果您只允许GET

get 'links/:id/dev', to: 'links#index'

You do not have to specify the parameters, these are parsed automatically. 您不必指定参数,这些参数将自动解析。

match '/links/:id/dev', to: 'links#index', via: :get

Will work fine. 将正常工作。 Everything after the "?" “?”之后的所有内容 are considered parameters and are not considered when matching the URL. 被视为参数,并且在与URL匹配时不被考虑。

match 'links/:id/dev', to: 'links#index', via: :get

You don't need to be explicit while sending a GET request. 发送GET请求时无需明确。 You can send whatever you want in the GET request, and can receive in the method through the params . 您可以在GET请求中发送任何内容,也可以通过params在方法中进行接收。

match '/links/:id/dev' => 'controller#action', :via => [:get], :as => :get_links

然后,您可以使用javascript中的get_links_path()进行访问

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

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