0

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
  • 2
    What have you tried? Commented Mar 24, 2012 at 21:31
  • 1
    nothing special because I do not know Commented Mar 24, 2012 at 21:46

2 Answers 2

2

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 %>
...
Sign up to request clarification or add additional context in comments.

Comments

1

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.