1

I'm using Rails 3.0. I want to add a Javascript file show_javascript.js that is in /public/javascripts/ to a view show.html.erb.

The show.html.erb file takes the <head>...</head> part from the template application.html.erb

I'm wondering how I should add it.

1 Answer 1

1

Assuming you have a generic stylesheet layout you could add the following to application.html.erb:

<%= javascript_link_tag 'show' if params[:action] == 'show' %>

You could even use content_for params plus yield in the <head> section like so:

layout.html.erb
<head>
  <%= yield(:header) if @content_for_header %>
</head>

product/show.html.erb
<% content_for :header do -%>
  <%= javascript_link_tag 'show_product' %>
<% end -%>
Sign up to request clarification or add additional context in comments.

3 Comments

I think @Nathan has given the most perfect answer, if it is used only for show.html.erb, then using content_for params plus yield is the way to go. (its just that Nathan has to change his css tag to js tag :D)
@sameera207 fixed thanks, lol, don't know what I was thinking :P

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.