名为路线的铁路 +控制器作为参数

发布于 2024-09-03 04:40:42 字数 203 浏览 6 评论 0原文

是否可以将名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萤火眠眠 2024-09-10 04:40:42

Thomas 的 答案是正确的,但是如果您希望 URL 格式更加灵活,您可以指定多个路由并使用 < a href="http://guides.rubyonrails.org/routing.html#route-requirements" rel="nofollow noreferrer">路线要求,在每个路线上放置 :requirements 。仅当满足要求时,该路线才会匹配。例如:

map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /one/ }, :controller => 'one'
map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /two/ }, :controller => 'two'

然后:

/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:

map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /one/ }, :controller => 'one'
map.resources :rides, :path_prefix => '/:option', 
  :requirements => { :option => /two/ }, :controller => 'two'

and then:

/one/rides will go to OneController

/two/rides will go to TwoController

不回头走下去 2024-09-10 04:40:42

这是可行的:

map.ride 'ride/:controller', :action => 'ride'

/ride/first 将调用 FirstController#ride/ride/second 将调用 SecondController#ride >

This would work:

map.ride 'ride/:controller', :action => 'ride'

/ride/first would call FirstController#ride and /ride/second would call SecondController#ride

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文