0

I know this has been answered here: Javascript Include Tag Best Practice in a Rails Application, however I don't get something.

How do I specify what js file is in the :defaults tag? I tried to specify path to js instead of using tag.

In application.html.erb:

<head>
  <title>...</title>
  <%= csrf_meta_tags %>

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

In view:

<% content_for :head, javascript_include_tag('qEditor/http_cdn.quilljs.com_1.3.2_quill') %>

This file is in the app/assets/javascripts/qEditor and according to IDE the path is correct. I changed the application.js from //= require_tree . to //= require_directory . so the js file won't be loaded elsewhere. It doesn't work...

Could you help me to solve this? I am really new in rails. Thank you

UPDATE

application.html.erb:

<!DOCTYPE html>
<html>
<head>
  <title>....</title>
  <%= csrf_meta_tags %>
  <%= content_for :assets do %>
  <%end%>
  <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>

<body>

<div class="container">
  <%= link_to 'Home', root_path, class: 'btn btn-default' %>
  <button onclick="toggleTodos()" class="btn btn-default">Toggle TODO's</button>

  <% flash.each do |key, value| %>
      <div class="alert alert-<%= key %>"><%= value %></div>
  <% end %>


  <div class="jumbotron">
    <%= yield %>
  </div>

</div>

</body>
</html>
Wlad
  • 410
  • 1
  • 9
  • 27
  • You could stub the js file to load it manually, then add it to your assets.rb file in the `Rails.application.config.assets.precompile` and then finally add it in your view using a `javascript_include_tag`. – Sebastián Palma Oct 14 '17 at 10:24
  • Stubbing is not needed if you place the file in `vendor/assets` where you should be placing vendor content anyways @SebastiánPalma – max Oct 14 '17 at 10:45
  • I see now, thanks for the tip @max. – Sebastián Palma Oct 14 '17 at 10:47
  • The main problem is that the line in the view is incorrect. Use `content_for` with a block and don't forget to use the `javascript_include_tage` with the `<%=` instead of `<%` tag, otherwise the code will not output in your view. See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method – 3limin4t0r Oct 14 '17 at 11:20

1 Answers1

2

The correct place for vendor content (anything that is not project specific or authored by you) is in /vendor/assets/javascripts. This also gets around your problem since vendor/assets is not required by //= require_tree ..

So move it to /vendor/assets/javascripts/http_cdn.quilljs.com_1.3.2_quill.js.

/vendor/assets is on Sprockets default assets lookup paths.

<head>
  <title>...</title>
  <%= csrf_meta_tags %>
  <%= content_for :assets do %>
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  <% end %>
</head>

Since content_for concatenates you can just add a content_for(:assets) to add additional content:

<% content_for :assets do %>
  <%= javascript_include_tag('http_cdn.quilljs.com_1.3.2_quill') %>
<% end %>

This also lets you use provide or concat: false in case you want to overwrite the block provided by the layout.

Brad
  • 8,044
  • 10
  • 39
  • 50
max
  • 96,212
  • 14
  • 104
  • 165
  • I thought it's working, but it isn't... sorry. Could you check my application.html.erb if it's correct? – Wlad Oct 14 '17 at 13:14
  • ”Its not working” is the most useless information in the world. What specifically is that is not working? You have to debug your application yourself. – max Oct 14 '17 at 15:11
  • What is not working is that js file isn't loaded in the view. I suspect I don't really understand how should I incoporate "<%= content_for :assets do %>" in application.html.erb because other parts are quite clear. – Wlad Oct 14 '17 at 15:34
  • Just replace the `` section with the part from my answer. I though that was pretty obvious. – max Oct 14 '17 at 22:15