Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Downloading files with multiple dots in filename (uploaded using Carrierwave)

The view:

<%= link_to File.basename(attachment.attachment.url), "/uploads/#{attachment.id}/#{File.basename(attachment.attachment.url)}" %>

The controller:

# ...
  def download
    path = "#{Rails.root}/uploads/"+ params[:id] + "/"+ params[:basename] +"."+ params[:extension]

    send_file path, :x_sendfile=>true
  end
# ...

The route:

match "/uploads/:id/:basename.:extension", :controller => "attachments", :action => "download", :conditions => { :method => :get }

The error is get is:

Routing Error

No route matches [GET] "/uploads/38/Screen_shot_2012-02-18_at_2.20.49_PM.png"
like image 799
wenbert Avatar asked Dec 04 '25 11:12

wenbert


1 Answers

match "/uploads/:id/:filename.:extension", :controller => "attachments", :action => "download", :constraints => { :filename => /[^\/]+/ },  :conditions => { :method => :get }

Thanks to forker I was led to this blog: http://coding-journal.com/rails-3-routing-parameters-with-dots/

like image 101
wenbert Avatar answered Dec 07 '25 05:12

wenbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!