0

I have added assets for a controller and wants to include them per request.

The problem is that it does not include that assets without writing .js in controller path

The code on layout page is:

<%= controller_specific_js = "#{params[:controller]}.js" %>
<%= javascript_include_tag controller_specific_js if asset_exist?
(controller_specific_js) %>

Helper method:

def asset_exist?(path)
        if Rails.configuration.assets.compile
          Rails.application.precompiled_assets.include? path
        else
          Rails.application.assets_manifest.assets[path].present?
        end
      end

The assets are in admin/popular_schools that are admin/popular_schools.js.coffee

<%= controller_specific_js = "#{params[:controller]}.js" %>

The above line outputs: admin/popular_schools.js and adds it.

URL is: http://localhost:3000/admin/popular_schools

assets.rb:

Rails.application.config.assets.precompile += %w( admin/popular_schools.js.coffee )

production.rb

config.assets.compile = true

How exactly I can run it without specifying .js in "#{params[:controller]}.js" as conventially its is not good.

If I remove .js it does not insert it in assets but if I include it it starts to appear.

Also if I use below code:

<%=  controller_specific_js = "#{params[:controller]}" %>
  <%= javascript_include_tag controller_specific_js if asset_exist?(controller_specific_js) %>

It does work without specifying .js but if I do not want to include any controllers in assets.rb file they start to output exception that file is not included in the assets.erb but it does included in assets folder and for some reasons I do not want to include them in assets.rb

Any workaround or conventions?

  • What's wrong with checking for the `js` extension? – Eyeslandic Mar 03 '17 at 09:31
  • My concern is that using js is right or wrong convention? No one specify the js in controller line. So I am sort of confuse with it. Could you please elaborate? –  Mar 03 '17 at 09:37
  • I am trying with `Rails.application.config.assets.precompile += %w( admin/*.js admin/*.coffee )` but its also not working. Not including all from admin folder. –  Mar 03 '17 at 09:43
  • You have to add files individually to the precompile array. Why are you adding them like this though, much better to use a manifest file, usually called application.js and require the necessary files there. – Eyeslandic Mar 03 '17 at 09:51
  • What do you say, is it fine to check for js extension in layout? –  Mar 03 '17 at 09:59
  • I don't see why it should not be ok to check for the `js` extension, it's the standard extension for javascript files – Eyeslandic Mar 03 '17 at 11:24
  • I am getting exception because files do exists in directory but not included in assets.rb file as I do not want to add bulk of files in that file. So its cruical to add them in assets.rb as well? –  Mar 03 '17 at 11:35
  • So basically you mean I should have to add files individually to the precompile array? Means we cant skip some files from that? –  Mar 03 '17 at 11:36
  • You should ask a new question on here for this, you're straying from your original question. You'll get better responses that way. – Eyeslandic Mar 03 '17 at 11:41
  • But no one even answer my original question. –  Mar 03 '17 at 11:52
  • Well I did, you cannot run it without js as you yourself said and there's nothing wrong with including the js in the check – Eyeslandic Mar 03 '17 at 12:16

0 Answers0