A
A
Alexander Petrov2016-09-16 09:42:17
Ruby on Rails
Alexander Petrov, 2016-09-16 09:42:17

How to include certain files?

Rails has an assets folder. there lies scripts and styles. For each new controller, a new coffescript and a new scss are created.
Is it possible to make sure that if we use a specific controller, then application.js and the script of this particular controller are loaded?
And then I get a lot of files already and they are loaded on all pages. And I need to load certain scripts and application.js in certain pages, how can I implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Chronic 86, 2016-09-16
@Mirkom63

<%= javascript_include_tag params[:controller] %>

A
Andrey Demidenko, 2016-09-16
@Dem1

You can do this
in the layout, write
A on the connect page:

<% content_for :after_js do %>
  <%= javascript_include_tag 'pages/users/index' %>
<% end %>

Also forget to exclude the file from application.js
AND add it to config/initializers/assets.rb
Rails.application.config.assets.precompile += ['pages/users/index.js']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question