Guilherme
Guilherme

Reputation: 61

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?

Upvotes: 6

Views: 1295

Answers (1)

ErsatzRyan
ErsatzRyan

Reputation: 3043

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

Upvotes: 1

Related Questions