How to use javascript_include_tag to get all scripts in a folder

前端 未结 4 733
走了就别回头了
走了就别回头了 2021-02-08 18:13

I\'d like to use javascript_include_tag to grab all view related scripts using recursion, which a placed in public/javascripts/views.

I\'m tryi

相关标签:
4条回答
  • 2021-02-08 18:45

    A simple solution is to create a new js-file ( further called activate.js ) and include the files there.

    # < FolderWithFiles >/activate.js :
    
    //= require_tree . 
    
    0 讨论(0)
  • 2021-02-08 18:49

    You can use

    <%= javascript_include_tag :all %>
    

    But this will work only if the scripts are stored in your

    RAILS_ROOT/public/javascripts
    

    directory

    0 讨论(0)
  • 2021-02-08 18:56

    According to the API documentation of javascript_include_tag:

    Returns an html script tag for each of the sources provided.

    You can pass in the filename (.js extension is optional) of javascript files that exist in your public/javascripts directory for inclusion into the current page or you can pass the full path relative to your document root.

    You cannot recursively include all files in your directory. However, you always can write something like the next line:

    javascript_include_tag *Dir[Rails.root.join("public/javascripts/views/**/*.js")]
    
    0 讨论(0)
  • 2021-02-08 19:05

    The following (unoptimized) code should work in rails 3.0 or above

    <%= javascript_include_tag Dir[Rails.root.join("public/javascripts/views/**/*.js")].map { |s| s.sub(Rails.root.join("public/javascripts/").to_s, "") } %>
    
    0 讨论(0)
提交回复
热议问题