1

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 ?

Thaha kp
  • 3,689
  • 1
  • 26
  • 25
user1611830
  • 4,749
  • 10
  • 52
  • 89
  • It's not a hash, it's a symbol – Yevgeniy Anfilofyev Jul 18 '13 at 12:24
  • @YevgeniyAnfilofyev oh sorry I mistook ! I updated my post – user1611830 Jul 18 '13 at 12:27
  • I think it's worth a look at this answer http://stackoverflow.com/a/11447568/2422778 – Mike Szyndel Jul 18 '13 at 12:31
  • Rails use symbols in a lot of their stuff. It's just convention. You can use `:time => 15` or `'time' => 15` to pass a parameter, for example, and it will be the same. Rails even has it's own implementation of `Hash`, which they call `HashWithIndifferentAccess`, in which you can use symbols or strings **interchangeably** to access values. – Doodad Jul 18 '13 at 12:55

1 Answers1

3

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

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • ok I read the doc. Suppose I want to pass some variables by `POST` by the means of two inputs : params[:id] and whatever[:id]. Is it possible to pass two different values for `:id` ? – user1611830 Jul 18 '13 at 12:54
  • Every parameter goes to `params`. If you want to pass a Hash, it will be accessible through `params[:something]` (or `params['something']`). Like, `params[:whatever]` returns => `whatever[]`. Which then you can call whichever key you want. So you will first define `whatever[:id]` and then pass both, `:id` and `:whatever` through the `POST`. – Doodad Jul 18 '13 at 12:58
  • @Doodad so you can't use `:id` two times ? – user1611830 Jul 18 '13 at 13:09
  • Well, if you would go with a `GET` and put `?id=1&id=5` in the link, wouldn't one `id` overwrite the other? `:id` (which would work just the same if you wrote `"id"`) is just a way to tell rails the name of your parameter. You can't have two with the same name, AFAIK. You can use two different parameters (`id` and `whatever`), one you put your ID, and the other you put a hash in which the key `:id` will return another thing and I think it will be the closest you can get to what you described, as every parameter goes to `params` in Rails. – Doodad Jul 18 '13 at 13:13