1

I just want to add different CSS/JavaScript on different views. Obviously if I put them in layout, they will be added to each view. Also, I don't want to create separate layouts for different views.

I created a layout layouts/standard.html.erb with following content in head:

<head>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
</head>

This is adding no CSS/JavaScript from app/assets folder to the rendered page.

I also tried <%= yield :javascript %> in layout and content_for in the view using javascript_include_tag 'filename' but it is giving asset pipeline error.

1
  • Can you post the code in detail about what you tried? Commented Aug 5, 2015 at 8:21

1 Answer 1

1

You can use controller specific javascripts like described in the documentation:

When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the coffee-rails gem is in the Gemfile) and a Cascading Style Sheet file (or SCSS file if sass-rails is in the Gemfile) for that controller. Additionally, when generating a scaffold, Rails generates the file scaffolds.css (or scaffolds.css.scss if sass-rails is in the Gemfile.)

For example, if you generate a ProjectsController, Rails will also add a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss. By default these files will be ready to use by your application immediately using the require_tree directive. See Manifest Files and Directives for more details on require_tree.

I recommend looking through all of that page as well to understand how asset pipeline works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot EugZon, this solved lot of my questions

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.