我正在尝试创建播客页面。
我有mp3文件URL https://mcdn.podbean.com/mf/web/tcips9/Introverted.mp3
我想要有一个链接到mp3文件网址的下载按钮,所以当我点击它时,我想下载,而不是打开一个新的web浏览器并播放。
= link_to "Download", @podcast.episode_audio_url, download: "{@podcast.episode_audio_url}"我尝试了上面的代码,它打开了一个新的web浏览器并播放。
我如何实现我的目标?请帮帮我。
我的控制器
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提前谢谢你。
发布于 2020-06-11 04:49:54
我建议你在应用程序控制器中编写下载逻辑。所以在你的路线中你会有
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"https://stackoverflow.com/questions/62312550
复制相似问题