I develop a rails app with exercises (for kids with learning difficulties in math). The interactive part of the exercises is written in javascript. I store each exercise in a database. The javascript contains
<%= asset_path('to_images') %>
I can read the scripts into the controller and write the content to a partial, but I think it would be better to capture the scripts in a variable, like:
@animation = exercise.animation
where any code containing <%= asset_path(...) %> would be replaced with the correct fingerprinted route to the asset.
Here is an example of a code snippet in exercise.animation:
$("#hundred_square td").css({
backgroundImage: 'url(<%= asset_path("exercises/shapes/circles/circle_open_black_48.png") %>)',
backgroundSize: "2vw",
backgroundRepeat: "no-repeat",
backgroundPosition: "center"
});
I have already tried to
class Exercise < ActiveRecord::Base
include ActionView::Helpers::AssetUrlHelper
and
self.animation.gsub(/\<\%\=\s*asset_path\((.+)\)\s*\%\>/) do |match|
address = $1
puts "#{address}"
=> "exercises/shapes/circles/circle_open_black_48.png"
puts "#{asset_path(address)}"
=> /"exercises/shapes/circles/circle_open_black_48.png"
puts "#{ActionController::Base.helpers.asset_path(address)}"
=> /"exercises/shapes/circles/circle_open_black_48.png"
end
do not produce the result I need. Thanks for your suggestions!