joel1
(joel)
1
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
radar
(Ryan Bigg)
2
What’s wrong with doing this:
?
Phlip
(Phlip)
4
joel wrote:
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
%() 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 <script> 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 <script> tag on the outside, and the "unrolled loop" on the inside.