Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Rails Content_for / yield - duplicated content

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?

like image 725
Guilherme Avatar asked Oct 06 '11 17:10

Guilherme


1 Answers

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

like image 95
ErsatzRyan Avatar answered Nov 12 '22 14:11

ErsatzRyan