简体   繁体   English

Rails路由多个可选参数

[英]Rails routes multiple optional parameters

I have a question, how can you make multiple optional params. 我有一个问题,你如何制作多个可选的参数。 Fe in my variant, I need similar thing to "/(:first_name || :last_name || middle_name || all_names)". 在我的变体中,我需要与“/(:first_name ||:last_name || middle_name || all_names)”类似的东西。 How can I achieve and can I achieve such thing in routes? 我怎样才能实现并且可以在路线中实现这样的目标?

Btw, it'd be nice if I can do a lot of multiple params fe: 顺便说一下,如果我可以做很多多重参数,那就太好了:

/(:a || :b || :c)/(:d || :e)/(:n || :m)

Thanks for answering. 谢谢回答。

You'll have to do all the conditional work in the controller - the routes are there to capture request URLs & direct them to specific functionality (controller/actions). 您必须在控制器中执行所有条件工作 - 路由用于捕获请求URL并将其指向特定功能(控制器/操作)。

Thus, your question of using 因此,您的使用问题

/(:a || :b || :c)/(:d || :e)/(:n || :m)

... is fundamentally flawed (you can't have condition "or" in Routes). ......从根本上是有缺陷的(你不能在路线中有条件“或”)。


What you can have is bound parameters : 可以拥有绑定参数

在此输入图像描述

These are optional params which a route can take, but doesn't have to. 这些是路线可以采取的可选参数,但不是必须的。

In your case, you'll need to use them to denote the name as passed: 在您的情况下,您需要使用它们来表示传递的名称:

#config/routes.rb
resources :users, path: "" do
   get :first_name(/:middle_name(/:last_name)), action: :show, on: :collection
end

This is the best you're going to get without doing something custom in the routing system... like having slugs or something. 如果没有在路由系统中做一些自定义的事情,这是你将获得的最好的...就像有slugs or一样。

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

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