Skip to content

Commit 1cffe09

Browse files
committed
Update Photo upload url for add user login as photo path
1 parent 67c0e0b commit 1cffe09

File tree

4 files changed

+4
-3
lines changed

4 files changed

+4
-3
lines changed

app/controllers/photos_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def create
1010
end
1111

1212
# 浮动窗口上传
13-
@photo = Photo.new(image: params[:file], user_id: current_user.id)
13+
@photo = Photo.new(image: params[:file], user: current_user)
1414
if @photo.save
1515
render json: { ok: true, url: @photo.image.url(:large) }
1616
else

app/models/photo.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ class Photo < ApplicationRecord
55

66
# 封面图
77
mount_uploader :image, PhotoUploader
8+
after_commit :remove_image!, on: :destroy
89
end

app/uploaders/photo_uploader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class PhotoUploader < BaseUploader
55
def filename
66
if super.present?
77
@name ||= SecureRandom.uuid
8-
"#{Time.now.year}/#{@name}.#{file.extension.downcase}"
8+
"#{model.user&.login || Time.now.year}/#{@name}.#{file.extension.downcase}"
99
end
1010
end
1111
end

test/controllers/photos_controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
post photos_path, params: { file: file }
1212
assert_equal 200, response.status
1313
assert_equal true, response.parsed_body["ok"]
14-
assert_match Regexp.new("/uploads/photo/#{Date.today.year}/[a-zA-Z0-9\\-]+.png!large"), response.parsed_body["url"]
14+
assert_match Regexp.new("/uploads/photo/#{user.login}/[a-zA-Z0-9\\-]+.png!large"), response.parsed_body["url"]
1515
end
1616

1717
it "POST /photos failure for blank data" do

0 commit comments

Comments
 (0)