2017-08-24 232 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

它工作:)謝謝 –