Is it possible to render some part of the template if it is a certain page of the web site? Or is it also possible to include specific javascripts in application.html.erb layout if it is a certain page?
2 Answers
Maybe content_for can help you.
For example:
layout.html.erb
...
<head>
<%= yield :scripts %>
</head>
...
<%= yield %>
view.html.erb
...
<% content_for :scripts do %>
<script>..</script>
<% end %>
...
Comments
As an addendum to what railscard said I usually do this:
In the layout:
<%= stylesheet_link_tag content_for?(:stylesheets) ? yield(:stylesheets) : "application", :debug => Rails.env.development? %>
Then inside a view
<% content_for :stylesheets %> my_view.js <% end %>
That way you only have to set content_for if there is something special you want to change the top level file loaded from sprockets.
1 Comment
Robin
You should have a helper that takes any number of arguments (*args), and do the content_for for you, so that you can just write:
<% stylesheets "styleshee1", "stylesheet2" %> in your views.