简体   繁体   English

如何将控制器的所有操作映射到Rails的顶层?

[英]How do I map all actions for a controller to the top-level in Rails?

Whenever I develop sites using Rails, I create a controller called "front" which has actions like "index," "how," and "about." 每当我使用Rails开发站点时,我都会创建一个名为“ front”的控制器,该控制器具有诸如“ index”,“ how”和“ about”之类的操作。 So, the home page, "How It Works," and "About Us" pages can be accessed via /, /how/, and /about. 因此,可以通过/,/ how /和/ about访问主页“工作原理”和“关于我们”。 My routes look like this: 我的路线如下所示:

map.root :controller => "front", :action => "index"

map.connect 'how', :controller => 'front', :action => 'how'
map.connect 'about', :controller => 'front', :action => 'about'

I have two questions: 我有两个问题:

1) Is this a good organization? 1)这是一个好的组织吗?

2) Is there a way to add one route to make all actions in the "front" controller accessible via /[action]? 2)是否有一种方法可以添加一条路由,以使通过/ [action]访问“前”控制器中的所有操作?

I sometimes have this route as the very last one: 有时我将这条路线作为最后一条路线:

map.connect ':action', :controller => 'main'

...to handle the all-actions thing you mention. ...处理您提到的所有行动。 As for organization, I think it always depends on the app, what it is, etc. Throw in a little personally preference and I think you're on the right track :) 对于组织而言,我认为它始终取决于应用程序,应用程序的类型等。以个人偏好为准,我认为您的方向正确:)

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

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