Category: Rails

Ruby Tempfile extension without random postfix

Recently I fell into the "RFM" trap (read fu**ing manual). I had some Ajax files uploaded to my controller and to make it easier, I've been processing them using Tempfile class. This was not a problem (I've been processing their content) until I've decided to do an Ajax file upload which was connected to paperclip. Paperclip saves the file name with extension so when using with Tempfile instance, you might get the invalid file extension.

temp = Tempfile.new(params[:file_name])
temp.binmode
temp.write(Base64.decode64(file[:file_value]))
temp.rewind
temp

The file name is obtained with method path:

temp = Tempfile.new('demo.jpg')
temp.path #=> #<Tempfile:/tmp/demo.jpg20130403-3747-13d19dx>
user.avatar = temp
user.save!

# Paperclip object instance
user.avatar(:big) #=> '/images/users/user_avatar.jpg20130403-3747-13d19dx'

This might be a problem, because some browser won't render image with invalid extension. However fixing this issue is really easy. It all comes down to reading the manual.

You can provide an Array instance with two elements as a first parameter. The temporary file base name will begin with the array’s first element, and end with the second element:

temp = Tempfile.new(['demo', '.jpg'])
temp.path #=> #<Tempfile:/tmp/demo20130403-3747-13d19dx.jpg>
user.avatar = temp
user.save!

Conclusion

Since this was mentioned in documentation I've got a lesson to always read it :)

Ruby on Rails on Passenger and Redmine: incompatible character encodings: UTF-8 and ASCII-8BIT

After migrating Redmine to a new server, I've encountered such an error:

ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT):
    3: <div class="box tabular settings">
    4: <p><%= setting_select :ui_theme, Redmine::Themes.themes.collect {|t| [t.name, t.id]}, :blank => :label_default, :label => :label_theme %></p>
    5: 
    6: <p><%= setting_select :default_language, lang_options_for_select(false) %></p>
    7: 
    8: <p><%= setting_select :start_of_week, [[day_name(1),'1'], [day_name(6),'6'], [day_name(7),'7']], :blank => :label_language_based %></p>
    9: <% locale = User.current.language.blank? ? ::I18n.locale : User.current.language %>
  app/helpers/settings_helper.rb:40:in `setting_select'
  app/views/settings/_display.html.erb:6:in `block in _app_views_settings__display_html_erb__83012438_101817890'
  app/views/settings/_display.html.erb:1:in `_app_views_settings__display_html_erb__83012438_101817890'
  app/views/common/_tabs.html.erb:24:in `block in _app_views_common__tabs_html_erb__985403460_101198110'
  app/views/common/_tabs.html.erb:23:in `each'
  app/views/common/_tabs.html.erb:23:in `_app_views_common__tabs_html_erb__985403460_101198110'
  app/helpers/application_helper.rb:263:in `render_tabs'
  app/views/settings/edit.html.erb:3:in `_app_views_settings_edit_html_erb__815292708_88944890'
  app/controllers/settings_controller.rb:26:in `index'

My database and all my configs were set to UTF8, so it looked like the problem was somewhere in Apache. Partialy I was right. To fix this issue, you need to add following lines:

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

to /etc/apache2/envvars and then just restart server:

# /etc/apache2/envvars
/etc/init.d/apache2 restart

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑