views:

115

answers:

1

Hi All,

I have a section of code in a controller that replaces existing HTML with an IMG tag. The code is as follows:

 render :update do |page|
   page.replace_html "chart-div", "<img src=\"#{chart.chart_file}\"/>"  #chart.chart_file is a path
 end

For whatever reason, I keep receiving the following error:

  ActionController::RoutingError (No route matches "/public/charts/1_WEEKLY_ACTUAL_LINE.jpg" with {:method=>:get}):

I have NO idea why it's assuming I want to route somewhere. It seems that I must have the "public" on the beginning in order for the file to be properly created, however I must remove "public" in order to display the image. Any thoughts? Is there a more standard mechanism by which to deal with dynamically created images/items?

Best.

NOTE: Please no "upload" plugins. All files are created by the system, there are no uploads.

+3  A: 

When adding the file, you are adding it to the file system, where it is located at RAILS_ROOT/public/charts/1_WEEKLY_ACTUAL_LINE.jpg.

When you want to display the file, you need a URL that points at it. Files stored in the public directory are accessed by their path relative to the public directory.

You might try something like this:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_url
    chart_file.gsub(%r{^/public}, "")
  end
end

Or, you can store the URL in the database, and do:

class Chart < ActiveRecord::Base # or whatever the chart class is
  def chart_file
    "/public#{chart_url}"
  end
end
Yehuda Katz