问题
I figured out how to use content_for
for some elements, for example <title>
:
<% content_for :title, "Hi, I'm #{@user.username}" %>
But I can't seem to get it working when I need to inject a <script>
tag with src
attribute. I.e. this code:
<script src="https://js.stripe.com/v3/"></script>
How can I use content_for
to place a script tag (with src
attribute) in the head
of a page?
回答1:
Answer from another forum
Add this to your head
<%= yield :whatever_name_you_want %>
And add this to your template/view:
<% content_for :whatever_name_you_want do %>
<script src="https://js.stripe.com/v3/"></script>
<% end %>
来源:https://stackoverflow.com/questions/65138243/use-content-for-with-src-attribute-of-a-script-tag