Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filename on update #2731

Closed
tigus opened this issue Mar 18, 2024 · 1 comment
Closed

filename on update #2731

tigus opened this issue Mar 18, 2024 · 1 comment

Comments

@tigus
Copy link

tigus commented Mar 18, 2024

I have an issue after upgrading to carrierwave 3 (from 2.2)
I wish to keep the previous name of my image on update, but it doesn't seem possible anymore.

I have a simple form to update an image of my img model:
<%= file_field_tag "asset-#{id}-img", class: "form-control field_img" %>

And then, in my controller, I make my update:

@asset.update(asset_params)

I use this function to setup the filename:

def filename
		if original_filename.present?
			if model.new_record?
				filename = File.basename(original_filename, '.*')
				"#{filename}#{filename.match(/_[0-9]{3}$/) ? '' : random_id}.#{file.extension}"
			else
				model['img']
			end
		end
	end

but, while carrierwave 2.2 returned the original name (saved in DB), model['img'] now returns a new name...

So I tried this code:

	def filename
		if original_filename.present?
			if model.new_record?
				filename = File.basename(original_filename, '.*')
				"#{filename}#{filename.match(/_[0-9]{3}$/) ? '' : random_id}.#{file.extension}"
			else
				model['img'] = model.img_was
			end
		end
	end

but it still doesn't work as, if my name was original_123.jpg, I get original_123(2).jpg...

Thanks in advance for your help!

@mshibuya
Copy link
Member

mshibuya commented Sep 7, 2024

As I wrote in this comment, you can choose to override #deduplicated_filename to stop the deduplication:

  def deduplicated_filename
    filename
  end

but I don't recommend this, the reason is in the comment.

@mshibuya mshibuya closed this as completed Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants