2

Going through Hartl's Rails tutorial, and he mentions that while:

stylesheet_link_tag "application", { media: "all",
                                 "data-turbolinks-track" => true } 

is valid

stylesheet_link_tag "application", { media: "all",
                                 data-turbolinks-track: true }

is not valid because "invalid because of hyphens." Can anyone explain this further for me? Why do hyphens make this invalid?

sawa
  • 165,429
  • 45
  • 277
  • 381
avinj86
  • 35
  • 1
  • 5

2 Answers2

1

Colon hash syntax does not support all symbols. Sometimes you must use hashrockets. Have a look here.

foo: true boils down to :foo => true. In symbols, dashes must be escaped like that: :'foo-bar'. foo-bar: true is not valid because :foo-bar => true is not, too.

atamanroman
  • 11,607
  • 7
  • 57
  • 81
0

This is because the hyphens could be interpreted as minus sign, and that's why variable names can't contain hyphens. I think you could do this though: :'data-turbolinks-track' =>

I know if you want to puts strange character in a symbol, you can do this: :'a-symbol^with#weird+:symbols'. That basically means wrap symbols in a string, except the ':' when putting unusually characters in them.

Addison
  • 3,791
  • 3
  • 28
  • 48
  • 1
    No, `'data-turbolinks-track': true` will not work. `:'data-turbolinks-track' => true`, however, will work. – mu is too short Jul 06 '14 at 04:01
  • @muistooshort Is this Rails/js/json specific behaviour? I know only basics of Rails but, as fair I remember, `'text': true` is equivalent of `:'text' => true`. I think it was added around version 1.9, so both codes you have written should be valid in the Rails 3/4. – Darek Nędza Jul 06 '14 at 09:03
  • No, the syntaxes are not at all equivalent: [1](http://stackoverflow.com/a/10004344/479863), [2](http://stackoverflow.com/a/8675314/479863), [3](http://stackoverflow.com/a/8796799/479863), [4](http://stackoverflow.com/a/9694676/479863). Nothing to do with Rails either, just a half-baked named argument implementation in Ruby. – mu is too short Jul 06 '14 at 17:39