Include once in partials


#1

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


#2

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 %>

<% end %>

  1. The flash hash:

<% unless flash[:included_once]
flash[:included_once] = true %>

<% end %>

  1. 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