Understanding usage of symbols in routes.rb files

后端 未结 1 1455
误落风尘
误落风尘 2021-01-25 11:04

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 :

相关标签:
1条回答
  • 2021-01-25 11:45

    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

    0 讨论(0)
提交回复
热议问题