我是红宝石的新手,我正在做一个项目。在 routes.rb 文件中,有一些我不明白的语法。更准确地说,我可以在这个文件中找到:
get :account #with a symbol
get 'notes' #with no symbol
有什么区别 ?我想:account
应该在其他地方定义,在路由过程中更早,对吧?
我是红宝石的新手,我正在做一个项目。在 routes.rb 文件中,有一些我不明白的语法。更准确地说,我可以在这个文件中找到:
get :account #with a symbol
get 'notes' #with no symbol
有什么区别 ?我想:account
应该在其他地方定义,在路由过程中更早,对吧?
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.
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.
Edit: If it's still confusing you can read this pretty complete guide on symbols in Ruby: http://www.troubleshooters.com/codecorn/ruby/symbols.htm