名为路线的铁路 +控制器作为参数
是否可以将名为route的rails中的控制器值作为参数,我可以在运行时传递该参数以将调用定向到正确的控制器?
例如 map.ride '骑行' ,:controller => {某种指定参数的方法},:action => 'ride'
然后在运行时,我想传递此调用应该去的控制器名称。我的操作正在执行不同的操作,具体取决于发送呼叫的控制器。 谢谢
is it possible to have the controller value in a rails named route as a parameter, which I can pass at runtime to direct the call to the proper controller?
e.g.
map.ride 'ride' ,:controller => {some-way-of-specifying-a-parameter}, :action => 'ride'
then at runtime, I want to pass the controller name to which this call should be going. My actions are doing different things depending to which controller the call gets sent.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Thomas 的 答案是正确的,但是如果您希望 URL 格式更加灵活,您可以指定多个路由并使用 < a href="http://guides.rubyonrails.org/routing.html#route-requirements" rel="nofollow noreferrer">路线要求,在每个路线上放置
:requirements
。仅当满足要求时,该路线才会匹配。例如:然后:
/one/rides
将转到OneController
/two/rides
将转到TwoController
Thomas' answer is correct, however if you want more flexibility in the URL format you can specify multiple routes and use route requirements by putting
:requirements
on each. The route will only match if the requirements are met. For example:and then:
/one/rides
will go toOneController
/two/rides
will go toTwoController
这是可行的:
/ride/first
将调用FirstController#ride
,/ride/second
将调用SecondController#ride
>This would work:
/ride/first
would callFirstController#ride
and/ride/second
would callSecondController#ride