Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coldnebo/4e9774486fb0c7a62617 to your computer and use it in GitHub Desktop.
Save coldnebo/4e9774486fb0c7a62617 to your computer and use it in GitHub Desktop.
capture doesn't work outside of the view it's in?
def broken_helper
render_to_string('another_partial', format: false)
end
def working_helper
%{alert('something');}.html_safe
end
<!-- view template -->
<% @myjs = capture do %>
<%= broken_helper %>
<% end %>
<%= @myjs %>
<!-- output: "something" -->
<!-- alternate approach -->
<% content_for(:javascript) do %>
<%= broken_helper %>
<% end %>
<!-- working approach -->
<% content_for(:js2) do %>
<%= working_helper %>
<% end %>
<!-- layout template -->
<script><%= @myjs %></script>
<!-- output: "" -->
<script><%= yield(:javascript) %></script>
<!-- output: "" -->
<script><%= yield(:js2) %></script>
<!-- output: "alert('something');" -->
@coldnebo
Copy link
Author

built this out into a test repo: coldnebo/rails_content_issue@3f4b135#diff-c41b830814781a1cd840bfed7a09e6b8R15

confirmed that the problem is in Rails3, but fixed in Rails4. see https://github.com/coldnebo/rails_content_issue/blob/master/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment