了解route.rb文件中符号的用法 [英] Understanding usage of symbols in routes.rb files
问题描述
我是红宝石的新手,并且正在从事一个项目。在routes.rb文件中,有一些我不理解的语法。更精确地讲,我可以在此文件中找到:
I am newbie to ruby, and I am working on a project. In the routes.rb file, and there is some syntax I don't understand. More precisely, I can find in this file alternatively :
get :account #with a symbol
get 'notes' #with no symbol
有什么区别?我想:account
应该在路由过程中的其他地方定义,对吗?
What is the difference ? I suppose :account
should be defined elsewhere, sooner in the routing process, right ?
推荐答案
get:account
(使用符号)和 get'account'
(使用字符串)正是在这种情况下也是一样。在您的路线中,Rails会将符号转换为字符串。
get :account
(using symbol) and get 'account'
(using string) are exactly the same in this context. In your route the symbol will be translated to a string by Rails.
这只是一种编码样式,我个人使用这些符号是因为我喜欢在IDE中查看颜色,它可以帮助我更快地阅读代码。
It's just a coding style, I personally use the symbols because I like to see the colors in my IDE, it helps me reading my code faster.
回答另一个问题:不需要在任何地方定义符号,这些符号不是方法或变量。您可以将它们视为一个常量,值等于它们的名称。
And to answer your other question: no you don't need to define symbols anywhere, those are not a method or a variable. You can see them as a constant with a value equal to their names.
编辑:如果仍然令人困惑,您可以阅读这份关于Ruby中符号的完整指南: http://www.troubleshooters.com/codecorn/ruby/symbols.htm
If it's still confusing you can read this pretty complete guide on symbols in Ruby: http://www.troubleshooters.com/codecorn/ruby/symbols.htm
这篇关于了解route.rb文件中符号的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!