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

Open
tigus opened this issue Mar 18, 2024 · 0 comments
Open

filename on update #2731

tigus opened this issue Mar 18, 2024 · 0 comments

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!

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

1 participant