在RoR中如何通过AJAX动态加载本地JS文件及配置路由?
Hey there! I’ve tackled similar asset loading issues with Revolution Slider in Rails before, so let’s walk through how to get those dynamic extensions loading properly. Here’s a step-by-step breakdown tailored to your scenario:
1. Organize Your Revolution Slider Assets First
First, let’s get your files in the right place to play nice with Rails’ asset pipeline:
- Drop the main
revolution.jsintovendor/assets/javascripts/(orapp/assets/javascripts/if you plan to modify it). - Create a subfolder like
vendor/assets/javascripts/revolution-extensions/and put all those dynamic plugin JS files there. This keeps things clean and makes asset pipeline management easier.
2. Route Configuration (If Needed)
Most of the time, Rails’ default static asset routing will handle files in assets/ or public/, but if Revolution Slider’s AJAX calls are targeting a custom path (like /revolution/extensions/some-plugin.js), you’ll need to map that path to your files.
Add a Route
Open config/routes.rb and add this route to catch extension requests:
# Map AJAX requests for Revolution extensions to our controller get '/revolution/extensions/:filename', to: 'static_files#serve_revolution_extension', constraints: { filename: /[^\/]+\.js/ } # Ensure we only match JS files
Create a Controller to Serve Files
Make a new controller app/controllers/static_files_controller.rb with this code:
class StaticFilesController < ApplicationController def serve_revolution_extension filename = params[:filename] # Point to your extensions folder file_path = Rails.root.join('vendor', 'assets', 'javascripts', 'revolution-extensions', filename) if File.exist?(file_path) # Send the JS file with the correct content type send_file file_path, type: 'application/javascript', disposition: 'inline' else # Return 404 if the file doesn't exist head :not_found end end end
3. Adjust Revolution Slider’s Extension Path
Next, you need to tell Revolution Slider where to look for those extensions. Find the slider initialization code in your index.html.erb or related JS file—look for an extensions configuration block. Update the path to match your route:
revapi1 = jQuery("#rev_slider_1_1").show().revolution({ // ... other slider config options extensions: { "extensionsPath": "/revolution/extensions/" // Match our route path } });
If you’re using the asset pipeline directly (no custom route), you can use Rails’ asset_path helper to generate the correct URL dynamically:
<%= javascript_tag do %> revapi1 = jQuery("#rev_slider_1_1").show().revolution({ // ... extensions: { "extensionsPath": "<%= asset_path('revolution-extensions/') %>" } }); <% end %>
4. Precompile Your Extensions (For Production)
To make sure these files are available in production, add them to the asset precompile list. Open config/initializers/assets.rb and add:
# Precompile Revolution Slider extension JS files Rails.application.config.assets.precompile += %w( revolution-extensions/*.js )
This tells Rails to process these files during rails assets:precompile so they’re ready for deployment.
5. Test and Troubleshoot
- Check Network Requests: Open your browser’s dev tools, go to the Network tab, and reload the page. Look for the AJAX requests for the extension JS files—if you see 404s, double-check your file paths and route configuration.
- Dev Environment Settings: Ensure
config/environments/development.rbhasconfig.serve_static_assets = true(it’s default in Rails 5+). - Restart Server: After making changes to routes or asset config, restart your Rails server to apply them.
That should cover everything you need to get Revolution Slider’s dynamic extensions loading via AJAX in your Rails app!
内容的提问来源于stack exchange,提问作者Mateo

