Forum: Ruby on Rails -%> (hyphen character) in .js.erb

Posted by shellz (Guest)
on 2013-02-07 16:18
(Received via mailing list)
Post reply
[image: More message actions]
 10:27 PM (1 minute ago)
  When I run rake assets:precompile on my app, I get the following 
error:

syntax error, unexpected tSTRING_BEG, expecting 
keyword_end_erbout.concat
"\n"

which refers to a line in a .js.erb file ending in -%>.

The ending hyphen was added to eliminate the line break in .html.erb 
files.
But when I remove the hyphen, the error goes away. The last sentence 
ofsection'2.2.1
CSS and ERB' in 
<http://guides.rubyonrails.org/asset_pipeline.html>...
says 'Note that the closing tag cannot be of the style -%>.' Does anyone
know why adding the dash before %> in .js.erb (and apparently in 
.css.erb)
files causes the rails precompiler to fail?

Thanks!
Posted by Robert Walker (robert4723)
on 2013-02-07 19:20
shellz wrote in post #1095771:
> syntax error, unexpected tSTRING_BEG, expecting
> keyword_end_erbout.concat
> "\n"
>
> which refers to a line in a .js.erb file ending in -%>.
>
> The ending hyphen was added to eliminate the line break in .html.erb
> files.
> But when I remove the hyphen, the error goes away. The last sentence
> ofsection'2.2.1
> CSS and ERB' in
> 
<http://guides.rubyonrails.org/asset_pipeline.html>...
> says 'Note that the closing tag cannot be of the style -%>.' Does anyone
> know why adding the dash before %> in .js.erb (and apparently in
> .css.erb)
> files causes the rails precompiler to fail?

Because using -%> causes a syntax error, unexpected tSTRING_BEG to 
occur.

<<<Damn that was a smart-ass reply. Shame on me>>>

In my quick test case I got a syntax error, unexpected ')'
...rbout.concat(( 'Hello World' -).to_s); _erbout.concat "\");\...

Notice that the dash is still there trying to be concatenated in.

I don't really know exactly why, but it's obvious you cannot use -%> in 
js.erb or css.erb, so don't do it and your code wont fail.
Posted by Jason Fb (jasonfb)
on 2013-02-07 19:29
(Received via mailing list)
See the last sentence of section 2.2.1 of the Asset Pipeline guide: 
http://guides.rubyonrails.org/asset_pipeline.html
Posted by Jason Fb (jasonfb)
on 2013-02-07 19:40
(Received via mailing list)
Sorry, that was redundant.

It's because -%> needs to know how to remove whitespace from within the 
file itself, parsing & reading the content outside the replacement 
content. I don't know for sure but I'll bet the explanation is simply 
that this is too complicated in these pipeline files because whitespace 
works differently.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.