Forum: Ruby on Rails include once in partials

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

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?

Gábor
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:

_partial.rb:

<%= @controller.script_include %>

controller.rb:

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

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

Justin
This topic is locked and can not be replied to.