I have a rails 3 app using paperclip. Is there a way with paperclip to automatically rename all files as follows: All spaces converted to _ , for various app related reasons I want to eliminate spaces in file names.
Thanks
You can use before_post_process to alter the filename.
http://www.davesouth.org/stories/make-url-friendly-filenames-in-paperclip-attachments
Related
I am using tolk for translation, but tolk takes all my values from en.yml and dumpes them in es.yml overwriting the existing content .
There are some stuff i don't want to be overwritten , so when i am searching for the es translations, i want rails to look in both es.yml and es.defaults.yml
( and so, i can keep isolated what i generate with tolk, and what remais the same )
Is there a way i can do this?
Thanks
Rails loads every file in the config/locales/ directory, so it will probably already work like you're suggesting. You can even organize it further than that, according to the I18n Guide:
http://guides.rubyonrails.org/i18n.html#organization-of-locale-files
However, I think that with duplicate key structures, Rails will probably override the values of the earlier loaded (sorted by file name) locale file with the values of the later loaded file. So please try to avoid duplicate keys.
In a recent paperclip gem upgrade I noticed that the uploaded filenames are being modified and some characters are being changed to an underscore.
I don't not like the fact that it changes spaces to underscores. What's wrong with a space in a file name? The 90's are long over..
After some digging I found this undocumented option for has_attached_file:
:restricted_characters
The default value of this option seems to be this:
:restricted_characters => /[&$+,\/:;=?#<>\[\]\{\}\|\\\^~%# ]/,
The last one is a space. Remove it and you're good.
remove non-utf8 characters, downcase, removes spaces. Is there a builtin way in rails to make filenames friendly and safe before paperclip saves?
I am trying to upload a file in rails (using paperclip), and I want to process some of the file data before letting paperclip send it off to s3 storage. In my controller, I just grab the file parameter (which does give me a file) and then I try to read the lines into an array
csv_file = params[:activity][:data]
array = IO.readlines(csv_file.path)
The problem is, I'm only getting the last line of the file. I tried using .rewind, but still get just the last line.
I dislike readlines and I always use regular expressions. Try this.
End of line - \n
Handy block structure to ensure that the file handle is closed:
File.open(csv_file.path) do |f|
a = f.readlines
process a...
end
Reading a whole file into memory might not be a good idea depending on the size of the files.
I upload/save mp3 files through Paperclip, it transforms the name with underscores when it saves it.
For example if I upload "Gould Stokowski 1.mp3" it saves into the the db as "Gould_Stokowski_1.mp3". How can I take out the underscores (replace them with spaces" when I retrieve the file and I want to display the name.
What does the program do with the characters that started out as underscores? If it does nothing, then there is no way to go back using just the file name. The names don't "round trip."
If you're not concerned with that, then your question really has nothing to do with Paperclip or MP3 files at all. You just need to know how to change all the underscores into spaces. You can use String#tr for that:
$ irb
>> "Gould_Stokowski_1.mp3".tr('_', ' ')
=> "Gould Stokowski 1.mp3"