0

I have the following code:

<table id = "mytable">
   <tbody>
        <% @event.each do |event| %>
           <!-- call javascript function here -->
         do_stuff ( <%= event %>)
        <% end %>
    </tbody>
</table>

The result is:

do_stuff(#<Event:0x1118119d> do_stuff(#<Event:0x432c632f> do_stuff(#<Event:0x1723c51d> do_stuff(#<Event:0x2d61fec9> do_stuff(#<Event:0x7c9e5565> do_stuff(#<Event:0x7ef899cd> do_stuff(#<Event:0x7339a2da> 

What is the correct syntax to call the javascript function do_stuff?

3
  • 1
    What argument you want to pass to do_staff? Commented Oct 28, 2013 at 13:31
  • do_stuff is a method or what..? Commented Oct 28, 2013 at 13:44
  • I want to pass each ruby on rails "event" into the javascript method, do_stuff, which is a javascript script method. This ought to be easy, no? Commented Oct 28, 2013 at 14:01

3 Answers 3

1

You need to wrap the javascript in script tags:

<script type="text/javascript">do_stuff('<%= event %>')</script>

And be sure that the entire event object is what you want to pass, because the javascript won't know what to do with it. Split it down into whatever event attributes you want to send:

<script type="text/javascript">do_stuff('<%= event.attribute1 %>', '<%= event.attribute2 %>')</script>
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest structuring this a little differently. By using something like the gon gem, you can make the @event variable visible to javascript functions. Then, do the whole loop in javascript, so you have less ruby code in your html view. You can loop over the array using plain javascript, or convenience functions from a library like jquery, lodash, or probably any other javascript library you happen to be using.

Comments

0

Thanks, in fact what's worked best is to create a javascript object, populate it from my ruby object and then call the javascript function I want to run:

  <script type="text/javascript">
      x = new Object();
      x.attr1 = "<%= event.attr1 %>";
      x.attr2 = "<%= event.attr2 %>";

      do_stuff(x);
  </script>

Comments

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.