Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:31:01+00:00 2026-05-27T10:31:01+00:00

TL;DR I’m trying to dynamically generate an asset URL using HTML5 data elements, on

  • 0

TL;DR

I’m trying to dynamically generate an asset URL using HTML5 data elements, on the client-side. This means the asset name isn’t available until runtime in the browser, which prevents ERB from pre-processing on the server.

Is there a work-around or best practice for handling such a situation?

Background

I’m using the Fusion Charts charting plugin in a Rails application, which uses various SWF files to generate charts. I’m in the process of upgrading the application to Rails 3.1 and have placed these SWF files into /vendor/assets/javascripts/fusion_charts.

Within the application, I dynamically specify the type of chart to draw using HTML5 data attributes, like so:

<%# ... metric.html.erb ... %>
<div id='chart-container' class='fusion-chart'
  data-chart-type='MSLine'
  data-source-url="<%= @metric.data_url %>">

Then I have a simple jQuery plugin written in CoffeeScript that pulls these data attributes and wires things to Fusion Charts:

// ... charting_plugin.js.coffee ...
chart = new FusionCharts
  swfUrl: "/assets/fusion_charts/#{data.chartType}.swf"
chart.setJSONUrl data.sourceUrl
chart.render containerId

The problem I’m running into is the swfUrl argument. To follow the guidance offered in the RoR Asset Pipeline guide, I should use the asset_path helper in ERB, but this won’t work because data.chartType is bound on the client, at run-time, and can’t be pre-processed server-side.

<# ... charting_plugin.js.coffee.erb: This won't work; data.chartType isn't defined ... %>
chart = new FusionCharts
  swfUrl: "<%= asset_path "fusion_charts/#{data.chartType}.swf" %>"
chart.setJSONUrl data.sourceUrl
chart.render containerId

Work Arounds

The simplest work-around (the one I’m using currently) is to simply move the SWF files back into /public/fusion_charts and not worry about the asset fingerprinting complexity of the pipeline. That said, I’m new to the pipeline, so maybe I’m missing something obvious.

Is there a better way to handle client-side asset URLs like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:31:01+00:00Added an answer on May 27, 2026 at 10:31 am

    One solution would be to have a lookup table for swf files by chart type, eg:

    var chartMap = {
    <% 
    chart_types.collect do |type|
      "#{type.to_json}: #{asset_path(type + '.swf')}"
    end.join(",\n")
    %>
    };
    

    Then add a function to get the correct path on the client side at runtime like so:

    function chartPath(type) {
      return chartMap[type];
    };
    

    Then change your setup call to be:

    chart = new FusionCharts
      swfUrl: chartPath(data.chartType)
    

    IE, use ERB to build a list of all possible paths you’ll need at asset compilation time, and then do a lookup at run time on the client. (and of course you’ll need to coffee script-ize the above – I’m not a coffee guy, I prefer latte’s. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.