我试图让我的用户能够下载声音文件。这是我的路线:
root 'welcome#index'
get 'sound/:id' => 'sound#download', :as => :download
resources :users, :sounds, :authentications
delete "authentications/:id" => "authentications#destroy"
声音模型在这里:
class Sound < ActiveRecord::Base
belongs_to :user
# for paperclip
has_attached_file :sound_file
# do not create a sound unless a sound file
# is present
validates_attachment_presence :sound_file
end
我在我的声音控制器中创建了一个下载功能:
def download
@sound= Sound.find(params[:id])
send_file @sound.sound_file.path,
:filename => @sound.sound_file_file_name,
:type => @sound.sound_file_content_type,
:disposition => 'attachment'
end
现在,当用户按下下载按钮时,应该下载声音文件。
<% @sounds.each do |sound| %>
...some code...
<%= button_to "Download", download_path(sound.id), method: :get %>
<br>
不幸的是,当用户按下按钮时,他或她会收到此错误:
没有路线匹配 [POST] "/sound/4"
很确定错误在路线中,但我不知道如何解决它。Paperclip 文档对下载的处理不多。我一直在查看 Stack Overflow 并找不到解决方案。有什么想法吗?