Dynamic url_host based on the url format - a private method called a string

I tried to do this for a long time, but I can not get around it.

For URLs ending in .pdf, I want to asset_hostretrieve files locally.

So, I am doing the following in a file development.rb

  config.action_controller.asset_host = Proc.new { |source,request|
    source.format.to_s.match(/pdf/) ? 
        "file://#{Rails.root.join('public')}" :
        "#{request.protocol}#{request.host_with_port}"
  }

But it gives me errors like:

private method `format ', called for" / stylesheets / css3buttons / reset.css? 1304745651 ": String

Question

What is the best way in rails3 to determine the URL request format?

Update I tried source, request.media_typeand request.format, but none of them extracted the PDF!

The logs are shown below. I have added debug statements for each of the three above.

  config.action_controller.asset_host = Proc.new { |source, request|
  print "Source:  " + source 
  print "\n"
  print "request.media_type: " + request.media_type
  print "\n"
  print "request.format:  " + request.format
  print "\n"
  }

: /stylesheets/css 3buttons/ reset.css?1304745651 request.media_type: request.format: text/html : /stylesheets/css 3buttons/css3-github-buttons.css?1304745651 request.media_type: request.format: text/html : /stylesheets/application.css?1304780110 request.media_type: request.format: text/html : /stylesheets/button -basics.css?1303711277 request.media_type: request.format: text/html : /stylesheets/global.css?1304739877 request.media_type: request.format: text/html : /javascripts/jquery.js?1302484024 request.media_type: request.format: text/html : /javascripts/rails.js?1302488107 request.media_type: request.format: text/html : /javascripts/jquery.maskedinput.js?1302484474 request.media_type: request.format: text/html : /javascripts/application.js?1300318225 request.media_type: request.format: text/html : /javascripts/jquery.js?1302484024 request.media_type: request.format: text/html : /javascripts/rails.js?1302488107 request.media_type: request.format: text/html : /javascripts/formToWizard.js?1256698412 request.media_type: request.format: text/html : /javascripts/application.js?1300318225 request.media_type: request.format: text/html : /javascripts/jquery.js?1302484024 request.media_type: request.format: text/html : /javascripts/rails.js?1302488107 request.media_type: request.format: text/html : /javascripts/rails _validations.js request.media_type: request.format: text/html : /javascripts/application.js?1300318225 request.media_type: request.format: /html

GET "/medicalhistories/9.pdf" 127.0.0.1 08 17:00:15 -0400 2011

pdf index.html.erb :

 <td><%= link_to "PDF", medicalhistory_path(medicalhistory, :format => "pdf")%></td>
+3
3

- "source.format". proc, config.action_controller.asset_host, source - , , . .

Soltuon # 1: , "pdf". :

source.ends_with? ( "PDF" )

№2 - request.media_type

№1 ... , . request.media_type mime /pdf . , , , .

+3

, action_controller. 3 :

1) httpd-, , , , URL-.

2a) , , , :

#routes
match '/medicalhistories/:id.pdf' => 'medicalhistories#pdf', :as => 'pdf_medicalhistory'

#MedicalhistoriesController

def pdf
  redirect_to "http://file_path"
end

2b) , Rails, #show, response_to {| act | act.pdf {||}}, : http://www.engineyard.com/blog/2010/render-options-in-rails-3/ ( redirect_to).

3) , , , . ( , proc_process, .)

, config.serve_static_assets = true ( 'show source' #call) , .

+1

#format ?

0

All Articles