Forum: Ruby on Rails include once in partials

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Gábor Sebestyén (Guest)
on 2006-03-16 18:22
(Received via mailing list)

I have to include javascript code into my partial but I should do it
once. How can I do that?
What is the rule of thumb?

Justin B. (Guest)
on 2006-03-16 18:35
(Received via mailing list)
You could try any of these - I'm not sure which might work:

1) An instance variable in your partial

<% unless @included_once
       @included_once = true %>
<script> ... javascript here </script>
<% end %>

2) The flash hash:

<% unless flash[:included_once]
      flash[:included_once] = true %>
<script> ... javascript here </script>
<% end %>

3) Finally, you could make it a method in your controller:


<%= @controller.script_include %>


def script_include
  unless @script_included
    @script_included = true
    "... string holding javascript "
     "" # empty string since already called once

Any of those may work - I would try them in the order listed. HTH!

This topic is locked and can not be replied to.