观点:
<%= link_to File.basename(attachment.attachment.url), "/uploads/#{attachment.id}/#{File.basename(attachment.attachment.url)}" %>
控制器:
# ...
def download
path = "#{Rails.root}/uploads/"+ params[:id] + "/"+ params[:basename] +"."+ params[:extension]
send_file path, :x_sendfile=>true
end
# ...
路线:
match "/uploads/:id/:basename.:extension", :controller => "attachments", :action => "download", :conditions => { :method => :get }
错误是获取:
Routing Error
No route matches [GET] "/uploads/38/Screen_shot_2012-02-18_at_2.20.49_PM.png"
答案 0 :(得分:4)
match "/uploads/:id/:filename.:extension", :controller => "attachments", :action => "download", :constraints => { :filename => /[^\/]+/ }, :conditions => { :method => :get }
感谢forker,我被引导到了这个博客:http://coding-journal.com/rails-3-routing-parameters-with-dots/
答案 1 :(得分:0)
match "/uploads/:id/:filename", :controller => "attachments", :action => download, :requirements => { :filename => /.*/ }