neelabh
neelabh

Reputation: 577

ruby vs js string concatenation in rails

so in my show.html.erb file for onf the controllers i have had

<% content_for :head do %>
<script type="text/javascript"> 
var filepath= "/flexpaper/" + <%=  @exam.filename.to_s %> + ".swf"

        var flashvars = { 
              SwfFile : escape(filepath), ....

now this seemed to be the workaround solution I used but when I tried to achieve the same result the other way around as follows

<% content_for :head do %>
    <script type="text/javascript"> 
    var filepath= <%= "/flexpaper/" +  @exam.filename + ".swf".to_s  %>

            var flashvars = { 
                  SwfFile : escape(filepath), ....

i.e to generate filepath in ruby rather than javascript I get undefined error for filepath. now i understand that is because of escape as it didn't get filepath as a string i.r "stuff.." rather something like stuff.. so the escape errors out. but I don't see why? any ideas?

P.s- also any suggestions for UJS style organisation. I have every other js residing somewhere in other file and included in header as needed except for this somewhat dynamic one. I can't have ruby code in included js files and didn't wanted to have a javascript controller as like ryan bates railscast to generate this minor script. So any other simple solution.

Upvotes: 2

Views: 1512

Answers (2)

Fernando Diaz Garrido
Fernando Diaz Garrido

Reputation: 3995

I see a couple of errors from your code, in bothe solutions you are missing the quotes that will make some text a string. In the first one you could do like this:

<% content_for :head do %>
  <script type="text/javascript"> 
    var filepath= "/flexpaper/<%=  @exam.filename.to_s %>.swf"

    var flashvars = { 
          SwfFile : escape(filepath), ....

And in the second one you are missing the quotes for the whole string

<% content_for :head do %>
  <script type="text/javascript"> 
    var filepath= "<%= "/flexpaper/" +  @exam.filename + ".swf".to_s  %>"

        var flashvars = { 
              SwfFile : escape(filepath), ....

Upvotes: 0

Benson
Benson

Reputation: 22847

Essentially, you're missing quotes in your javascript in the second version. After the ruby has run, the generated page contains a script tag with pure javascript. That javascript looks like this:

var filepath= /flexpaper/some_exam.swf
    var flashvars = { 
      SwfFile : escape(filepath), ....

Note that you're assigning a value to the filepath variable, but that it's not surrounded by quotes. It's not valid javascript. If you fix it like this, it should work:

<% content_for :head do %>
  <script type="text/javascript"> 
    var filepath= "<%= "/flexpaper/" +  @exam.filename + ".swf".to_s  %>";
    var flashvars = { 
       SwfFile : escape(filepath), ....

While I was at it, I added a semicolon and fixed your indenting -- you'll find that properly indenting your javascript code makes it orders of magnitude easier to debug. That ruby code will produce javascript that looks about like this:

  <script type="text/javascript"> 
    var filepath= "/flexpaper/some_exam.swf";
    var flashvars = { 
       SwfFile : escape(filepath), ....

A trick to spot errors like this is to do a view-source in your browser, and look carefully at the javascript that's being produced.

Upvotes: 2

Related Questions