在 RoR 中设置静态上传路径

马利克·穆达萨

问题

我在videos_controller.rb 中有这两个函数,用于在 RoR 中创建和更新视频

# POST /videos
# POST /videos.json
def create
@video = Video.new(video_params)

respond_to do |format|
  if @video.save
    format.html { redirect_to @video, notice: 'Video successfully created.' }
    format.json { render :show, status: :created, location: @video }
  else
    format.html { render :new }
    format.json { render json: @video.errors, status: :unprocessable_entity }
  end
end
end

# PATCH/PUT /videos/1
# PATCH/PUT /videos/1.json
def update
respond_to do |format|
  if @video.update(video_params)
    format.html { redirect_to @video, notice: 'Video successfully updated.' }
    format.json { render :show, status: :ok, location: @video }
  else
    format.html { render :edit }
    format.json { render json: @video.errors, status: :unprocessable_entity }
  end
end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_video
    @video = Video.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def video_params
    params.require(:video).permit(:title, :file)
  end
end

问题是无论何时上传,它都会在上传/视频/文件/{id}/文件名的路径中创建一个新文件夹。

要求我希望路径是静态的,因为只有一个视频,所以我希望视频名称和路径即使更新也保持不变,即新文件(如果编辑)放置或保存在旧的旧文件名文件路径。

我应该怎么办?

中央电视台

如果您使用载波上传器,您可以更改store_dir方法uploaders/video_uploader.rb

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{model.id}"
end

但我不太明白,为什么默认行为会打扰你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章