Understanding usage of symbols in routes.rb files

你。 提交于 2019-12-02 08:52:20

问题


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

What is the difference ? I suppose :account should be defined elsewhere, sooner in the routing process, right ?


回答1:


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



来源:https://stackoverflow.com/questions/17723304/understanding-usage-of-symbols-in-routes-rb-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!