我正试图创建播客页面。
我有MP3文件的URL https://mcdn.podbean.com/mf/web/tcips9/Introverted.mp3
并希望有一个链接到MP3文件URL的下载按钮,所以当我点击它时,我想下载而不是打开一个新的网络浏览器和播放。
= link_to "Download", @podcast.episode_audio_url, download: "{@podcast.episode_audio_url}"
我试过上面的代码,但它是打开一个新的网络浏览器和播放。
我如何实现我的目标? 请帮助,。
我的控制器
class PodcastsController < ApplicationController
before_action :set_podcast, only: [:show, :edit, :update, :destroy]
# GET /podcasts
# GET /podcasts.json
def index
@podcasts = Podcast.all
end
# GET /podcasts/1
# GET /podcasts/1.json
def show
end
# GET /podcasts/new
def new
@podcast = Podcast.new
end
# GET /podcasts/1/edit
def edit
end
# POST /podcasts
# POST /podcasts.json
def create
@podcast = Podcast.new(podcast_params)
respond_to do |format|
if @podcast.save
format.html { redirect_to @podcast, notice: 'Podcast was successfully created.' }
format.json { render :show, status: :created, location: @podcast }
else
format.html { render :new }
format.json { render json: @podcast.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /podcasts/1
# PATCH/PUT /podcasts/1.json
def update
respond_to do |format|
if @podcast.update(podcast_params)
format.html { redirect_to @podcast, notice: 'Podcast was successfully updated.' }
format.json { render :show, status: :ok, location: @podcast }
else
format.html { render :edit }
format.json { render json: @podcast.errors, status: :unprocessable_entity }
end
end
end
# DELETE /podcasts/1
# DELETE /podcasts/1.json
def destroy
@podcast.destroy
respond_to do |format|
format.html { redirect_to podcasts_url, notice: 'Podcast was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_podcast
@podcast = Podcast.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def podcast_params
params.require(:podcast).permit(:episode_url, :episode_title, :episode_description, :episode_audio_url, :episode_number)
end
end
先谢谢你。
我建议你在应用程序控制器中编写下载逻辑。所以在你的路由中,你会有
get download_podcast, to: "downloads#podcast"
你的控制器会是
class DownloadsController
def podcast
send_file Podcast.find(params[:podcast_id]).episode_audio_url, type: "audio/mp3"
end
end
而您的观点将与这个动作联系起来
link_to "Download", download_podcast_path(podcast_id: @podcast.id), target: "_blank"