2017-08-24 235 views
0

问题设置静态上传路径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 

的问题是每当它上传,它会创建一个路径中的新文件夹是uploads/video/file/{id} /文件名。

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

我该怎么办?

+0

您用于管理上传的库区是什么? – cnnr

回答

1

如果您在使用carrierwave uploader,你可以在你uploaders/video_uploader.rb

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

改变store_dir方法

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

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

+0

它工作:)谢谢 –