Dynamically add javascript tag to a view

Hi everybody,

I would like to add javascript tags dynamically to my view. So I used
the following code :

@events.each do |e|

      %(javascript_tag 'new

Popup(‘event_popup_#{e.id}’,‘event_link_#{e.id}’)’)

end

However it seems the code is not being added to my DOM.

How can I correctly code this ?

Thanks in advance.

Joel

What’s wrong with doing this:

?

On Fri, Mar 28, 2008 at 11:00 PM, joel [email protected] wrote:


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Yes of course :wink: !

Thank you very much…

Joel

joel wrote:

end
%() declares a string literal, so nothing happens to your
javascript_tag. And
.each won’t return your string - even the correct one.

Try this eRB:

<%=
@events.map do |e|
javascript_tag “new
Popup(‘event_popup_#{e.id}’,‘event_link_#{e.id}’);”
end
%>

That still has its own style issues, including it will push several

tags in, not just one efficient one. You could also try this: <%= javascript_tag @events.map{|e| "new Popup('event_popup_#{e.id}','event_link_#{e.id}');" }.join %> So that puts the tag on the outside, and the "unrolled loop" on the inside. -- Phlip

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs