Rails 控制器名称使用破折号而不是下划线
是否可以强制 Rails 在引用控制器时使用破折号 (-) 而不是下划线。
目前,Inflector
存在一个很好的函数,称为parameterize
。它允许非常好的永久链接,删除所有特殊字符并用破折号替换...
但是,当使用具有多个单词的控制器时(例如 contact_methods_controller.rb
),您可以定义您的路线:
resources :contact_methods
这会创建到 /contact_methods
的映射(不是 /contact-methods
)。当我混合这两个时,我会得到丑陋的 URL,例如:
/contact_methods/1-preferred-email
我希望 Rails 地图控制器带有破折号而不是下划线。我所有的研究都说要单独映射每个控制器:
match 'contact-methods(/:action)' => 'contact_methods'
但在我看来,这真的很愚蠢,如果我嵌套资源,它就会变得混乱......我不应该将它们定义为自定义路由。 ActionDispatch 中是否有一个设置可以自动重写这些内容?我找不到一个...
Is it possible to forces Rails to use dashes (-) instead of underscores when referring to controllers.
There currently exists a nice function of the Inflector
called parameterize
. It allows for very nice permalinks with all special characters removed and replaced with dashes...
However, when using controllers that have multiple words (like contact_methods_controller.rb
for example), you define your route:
resources :contact_methods
This creates a map to /contact_methods
(NOT /contact-methods
). When I mix these two, I get ugly URLs like:
/contact_methods/1-preferred-email
I'd like to have Rails map controllers with dashes instead of underscores. All my research says to individually map each controller:
match 'contact-methods(/:action)' => 'contact_methods'
but that is really stupid, in my opinion, and it becomes messy if I'm nesting resources... I shouldn't have to define these as custom routes. Is there a setting in ActionDispatch
that automatically rewrites these things? I can't find one...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的route.rb中
编辑:你必须指定
:as =>; ...
路径,否则ActionDispatch
会出现异常...In your route.rb
Edit: You have to specify the
:as => ...
path or elseActionDispatch
throws a fit...