简体   繁体   中英

Rails - Render asset javascript

Is there any chance to render a precompiled javascript file? The plain file is no problem with:

def iframe_communication
  render file: "/app/assets/javascripts/iframe_communication_module.js"
end

I tried assets_path or to include it with javascript_include_tag in the view, but no succeeds.

For clarification I don't want to add the script to my view. I try to render the javascript file for external access (adding into <script> tags from other domains).

Update

  • render file: "..." shows the file as expected but the uncompiled version
  • assets_path "/app/assets/javascripts/iframe_communication_module.js" returns "/app/assets/javascripts/iframe_communication_module.js" and not a path to the compiled asset
  • javascript_include_tag loads the script as expected (Not Rendering)

The Rails 4 asset pipeline compiles everything into one file, is there any chance to get a single file out of it?
Or is the better way to compile it manually?

Update 2
Found this : assets_path don't do what I expected. It only prepends and appends:

asset_path "application.js" # => /application.js  
asset_path "application", type: :javascript # => /javascripts/application.js

Got a working solution:

I don't need the file in the normal asset pipeline. So add

//= stub iframe_communication_module

to apps/assets/javascript/application.js .

To precompile the file automatically add

config.assets.precompile += %w( iframe_communication_module.js )

to config\\application.rb

To access it through a controller add

redirect_to ActionController::Base.helpers.javascript_path("iframe_communication_module")

to the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM