Issue with Rails Content_for / yield - duplicated content
Asked Answered
M

1

6

I'm trying to load some javascript in my application page using a named yield block, but the code is duplicated because of a generic yield that load my view pages. something like that:

-----Code present in views-----

<% content_for :bottom_scripts do %>
    <script type="text/javascript">
             Some Javascripts
    </script>
<% end %>

------Code in my application page-----

<div id = "body">
    <%= yield %>
</div>
<%= yield :bottom_scripts %>

The script code is printed twice, but I need it just printed in the second yield, any thoughts?

Mump answered 6/10, 2011 at 17:15 Comment(2)
is it printed once in the body and once outside? can you post a sample of the rendered page?Twerp
As a side note, I would suggest leaving the script tags outside the yield instead of creating multiple instances for each content_for :bottom_scripts.Blouse
S
1

you can use content_for in your layout instead of yield

when content_for is not passed a block it outputs the block stored at that identifier

in view:

<% content_for :foo do %>
   <p>Bar</p>
<% end %>

in layout:

<%= content_for :foo %>

http://apidock.com/rails/ActionView/Helpers/CaptureHelper/content_for

Sergu answered 12/10, 2011 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.