# javascript with embedded ruby

**URL:** <https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976>\
**Category:** rubyonrails-talk\
**Created:** [June 5, 2016, 10:14pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976 "2016-06-05T22:14:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 5, 2016, 10:14pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976/1 "2016-06-05T22:14:37Z")

</div>

Hi folks!

Im currently trying to generate diagrams with a js lib and rails. It doesn't work, though, since I can't figure out how to embed ruby code to supply the diagrams with data. My editor highlights the ruby code weirdly, too, with I take as a hint for wrong ruby syntax. The diagrams work if I use the pure-js sample data, but it REALLY dont want to input data sets with 200 items by hand.

The snippet in question is this one, it lives in an .html.erb file:

var hour = [&nbsp;&nbsp;&nbsp;&nbsp;\<% @raw.each do |raw| %\> &nbsp;&nbsp;&nbsp;&nbsp;[ \<%= if raw.status == "aufgewacht" %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= Time.parse(raw.timestamp).wday.to\_json %\>, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= raw.timestamp[15,2].to\_json %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> ], &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;]; (yeah, pretty ugly)

Some error messages; line 20 contains the "if"-conditional .../app/views/processed\_data/\_scatter.html.erb:20: syntax error, unexpected ')', expecting keyword\_then or ';' or '\n' ...nd= ( if raw.status == "woke" );@output\_buffer.safe\_concat('

app/views/processed\_data/\_scatter.html.erb:23: syntax error, unexpected keyword\_end, expecting ')' ...ffer.safe\_concat(' '); end ;@output\_buffer.safe\_concat(...

Does anyone know how to do this?

---

<div class="post-metadata">

**Author:** ![Marco\_Antonio\_Almeid](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Marco\_Antonio\_Almeid](https://discuss.rubyonrails.org/u/Marco_Antonio_Almeid)\
**Post date:** [June 5, 2016, 11:04pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976/2 "2016-06-05T23:04:29Z")

</div>

Hi Rynn,

> Hi folks!
> 
> Im currently trying to generate diagrams with a js lib and rails. It
> 
> doesn’t work, though, since I can’t figure out how to embed ruby code to
> 
> supply the diagrams with data. My editor highlights the ruby code
> 
> weirdly,
> 
> too, with I take as a hint for wrong ruby syntax.
> 
> The diagrams work if I use the pure-js sample data, but it REALLY dont
> 
> want to input data sets with 200 items by hand.
> 
> The snippet in question is this one, it lives in an .html.erb file:
> 
> var hour = [
> 
> ```
> <% @raw.each do |raw| %>
> 
> [ <%= if raw.status == "aufgewacht" %>
> 
> ```

It seems that your problem is here. You don’t want to output the if condition so you should actually have:

\<% if raw.status == “aufgewacht” %\>

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 5, 2016, 11:35pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976/3 "2016-06-05T23:35:06Z")

</div>

Marco Antonio Almeida wrote in post #1183823:

> Hi Rynn,
> 
> > The snippet in question is this one, it lives in an .html.erb file:
> > 
> > var hour = [ &nbsp;&nbsp;&nbsp;&nbsp;\<% @raw.each do |raw| %\> &nbsp;&nbsp;&nbsp;&nbsp;[ \<%= if raw.status == "aufgewacht" %\>
> 
> It seems that your problem is here. You don't want to output the if condition so you should actually have:
> 
> \<% if raw.status == "aufgewacht" %\>

Thank you! That does indeed get rid of the error messages. I wonder how i missed this. However, the code still doesnt work - there is no data displayed.

Maybe the syntax highlighting makes it a bit clearer. Compare the two attached snippets, one with the js/erb code (with a working var hour below it, to demontrate that the js is (probably) not the problem, and some reguler embedded ruby without js.

Attachments: [http://www.ruby-forum.com/attachment/11367/Auswahl\_004.jpg](http://www.ruby-forum.com/attachment/11367/Auswahl_004.jpg)[http://www.ruby-forum.com/attachment/11368/Auswahl\_006.jpg](http://www.ruby-forum.com/attachment/11368/Auswahl_006.jpg)

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [June 5, 2016, 11:42pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976/4 "2016-06-05T23:42:47Z")

</div>

> Marco Antonio Almeida wrote in post #1183823:
> 
> > Hi Rynn,
> > 
> > > The snippet in question is this one, it lives in an .html.erb file:
> > > 
> > > var hour = [ &nbsp;&nbsp;&nbsp;\<% @raw.each do |raw| %\> &nbsp;&nbsp;&nbsp;[ \<%= if raw.status == "aufgewacht" %\>
> > 
> > It seems that your problem is here. You don't want to output the if condition so you should actually have:
> > 
> > \<% if raw.status == "aufgewacht" %\>
> 
> Thank you! That does indeed get rid of the error messages. I wonder how i missed this. However, the code still doesnt work - there is no data displayed.
> 
> Maybe the syntax highlighting makes it a bit clearer. Compare the two attached snippets, one with the js/erb code (with a working var hour below it, to demontrate that the js is (probably) not the problem, and some reguler embedded ruby without js.
> 
> Attachments: [http://www.ruby-forum.com/attachment/11367/Auswahl\_004.jpg](http://www.ruby-forum.com/attachment/11367/Auswahl_004.jpg)[http://www.ruby-forum.com/attachment/11368/Auswahl\_006.jpg](http://www.ruby-forum.com/attachment/11368/Auswahl_006.jpg)

You may be getting tripped up by the fact that raw() is a view helper method, and you are using it within your block as a local variable. Try a different word, and see if that makes any difference.

Walter

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [June 6, 2016, 2:57pm UTC](https://discuss.rubyonrails.org/t/javascript-with-embedded-ruby/71976/5 "2016-06-06T14:57:32Z")

</div>

Walter Davis wrote in post #1183825:

> > > > var hour = [
> > 
> > i missed this.
> 
> You may be getting tripped up by the fact that raw() is a view helper method, and you are using it within your block as a local variable. Try a different word, and see if that makes any difference.
> 
> Walter

Thanks for your input!

Unfortunately it didnt solve my problem, but I finally figured it out:

I had a newline character appended to my status variable, so raw.status could never equal "aufgewacht". It does, though, with raw.status.chomp!
