vendredi 3 mars 2017

unable to determine if controller based asset exists without .js extension rails

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?

Aucun commentaire:

Enregistrer un commentaire