Problem with Rails Content_for / yield - duplicate content

I am trying to load some javascript on my application page using a named yield block, but the code is duplicated due to the overall profitability loading my view pages. something like that:

----- The code present in the views -----

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

------ Code on my application page -----

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

The script code prints twice, but I just need to print it in the second lesson, any thoughts?

+5
source share
1 answer

you can use content_forin your layout instead of crop

when a content_forblock is not transmitted, it displays the block stored in this identifier

in sight:

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

in layout:

<%= content_for :foo %>

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

+1

All Articles