8

I am trying to use insert an Javascript variable in the <%= %> tags but it prints the variable name verbose. Here is my code

<script>
     function getGraph(agency,device_id)
      {
            var i = document.createElement('img'); 
            i.src = '<%= show_graph_hcfcd_url('device_id') %>'; 
            $(graphDiv).appendChild(i); 
      }
</script>

Now the Problem is URL gets generated just fine except instead of value of device_id, 'device_id' appears in the rest url.

Any clues how to get over this?

6
  • Can you show the source for show_graph_hcfcd_url? Commented May 9, 2011 at 18:35
  • Remove the quotes. If it is a string variable, you would not surround it with quotes. Commented May 9, 2011 at 18:39
  • @Bryan Drewery: the code of show_graph_hcfcd_url is okay and it works. I have tested the url separately. Problem is to generate the correct url. Commented May 9, 2011 at 18:41
  • @Fareesh : If i remove those quotes it considers the device_id as ruby variable which is wrong. by putting quotes it puts the device_id verbose in the url. For now i am then using the replace() function in javascript to replace it with variable. Commented May 9, 2011 at 18:43
  • 1
    I meant the quotes surrounding the <%= %> Commented May 9, 2011 at 18:44

2 Answers 2

10

<%= %> will interpret the code on the server

and javascript variable is available to change on the client. So doing

<%= show_graph_hcfcd_url('device_id') %>

is probably not be the correct way of doing this.

You might want to try to put the url(s) in the data attibute of the element:

<div id="device_id" data-url="<%= show_graph_hcfcd_url(@device.id) %>">...</div>

<script>
     function getGraph(agency,device_id)
      {
            var i = document.createElement('img'); 
            i.src = $(device_id).data("url"); 
            $(graphDiv).appendChild(i); 
      }
</script>

also see

http://railscasts.com/episodes/324-passing-data-to-javascript

There's a good gem to do this called "gon"

Sign up to request clarification or add additional context in comments.

Comments

2

Here is how i have solved this for now:

<script>
     function getGraph(agency,device_id)
      {

            var i = document.createElement('img'); 
            var url = '<%= show_graph_hcfcd_url('device_id') %>'; //new change
            url = url.replace('device_id',""+(device_id*1000));   //new change
            i.src = url;
            $(graphDiv).innerHTML = "";
            $(graphDiv).appendChild(i); 
      }
  </script>

The lines in the bold show what i did. Basically replaced the "device_id" in the url with its value.

But I am still hoping for a more elegant solution.

1 Comment

I don't think there's a an elegant solution to this problem. My solution was to replace the actual url with the root url and append the path and parameter: var url = "<%= root_url %>graph_hcfcd/"+device_id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.