Mudassar Khani
Mudassar Khani

Reputation: 1479

Set static upload path in RoR

Problem

I have these two functions in videos_controller.rb for create and update a video in 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

The problem is whenever it uploads, it creates a new folder in the path which is uploads/video/file/{id}/filename.

Requirement I want path to be static, because there will only be one video so i want the video name and path to stay same even if it is updated, i.e. the new file (if edited) be placed or saved by the old filename at old file path.

What should i do?

Upvotes: 0

Views: 49

Answers (1)

cnnr
cnnr

Reputation: 1307

If you using carrierwave uploader, you can change store_dir method at your uploaders/video_uploader.rb from

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

to

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

But I don't quite understand, why default behavior bother you.

Upvotes: 1

Related Questions