I have a rails application that is being upgraded from Rails 2.3.5 to Rails 3. It uses attachment_fu for file uploads. We're trying to do this conversion without making DB changes, so I'd like to avoid changing to paperclip or carrierwave at this time.
我有一个rails应用程序正在从Rails 2.3.5升级到Rails 3.它使用attachment_fu进行文件上传。我们尝试在不进行数据库更改的情况下进行此转换,因此我希望此时不要更改为paperclip或carrierwave。
Has anyone succeeded in using attachment_fu with Rails 3 and Ruby 1.9.2? We're using the most recent version of attachment_fu that claims to be ok for rails 3 and ruby 1.9.2, but getting 'TypeError (can't convert nil into Integer):' on any forms that include a file upload.
有没有人成功使用attachment_fu与Rails 3和Ruby 1.9.2?我们正在使用最新版本的attachment_fu声称对rails 3和ruby 1.9.2没问题,但是在任何包含文件上传的表单上都会出现'TypeError(无法将nil转换为整数)。
All the answers to previous questions seem to be 'just switch to paperclip or carrierwave' as in: Attachment_fu or Paperclip for Rails3 or TypeError (can't convert nil into Integer):
以前问题的所有答案似乎都只是“切换到paperclip或carrierwave”,如:Attachment_fu或Paperclip for Rails3或TypeError(无法将nil转换为整数):
Thanks!
3 个解决方案
#1
7
I made the following changes and it worked
我做了以下更改,但它确实有效
attachment_fu.rb
def temp_path
p = temp_paths.first
if p.is_a?(ActionDispatch::Http::UploadedFile) # Rails 3.0.3 compatability fix
p.tempfile.path
else
p.respond_to?(:path) ? p.path : p.to_s
end
end
I also changed returning filename.strip do |name|
to filename.strip.tap do |name|
我也改变了返回的filename.strip do | name | to filename.strip.tap do | name |
init.rb
def make_tmpname(basename, n)
ext = nil
n ||= 0
sprintf("%s%d-%d%s", basename.to_s.gsub(/\.\w+$/) { |s| ext = s; '' }, $$, n, ext)
end
I made a fork on github with this changes https://github.com/debprado/attachment_fu
我在github上用这个改变做了一个分支https://github.com/debprado/attachment_fu
#2
6
attachment_fu patches Tempfile.make_tmpname in attachment_fu/init.rb, and it doesn't work in 1.9.2: sprintf("%d",nil) fails, and in 1.8.7 the result of this expression is "0".
attachment_fu修补attachment_fu / init.rb中的Tempfile.make_tmpname,它在1.9.2中不起作用:sprintf(“%d”,nil)失败,而在1.8.7中,该表达式的结果为“0”。
The fix is to insert a line in init.rb from:
解决方法是在init.rb中插入一行:
sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)
to
n ||= 0
sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)
You can find some of the discussion here https://github.com/technoweenie/attachment_fu/issues/25
你可以在这里找到一些讨论https://github.com/technoweenie/attachment_fu/issues/25
Cheers!
#3
3
Try my gemified version that supports Rails 3.2:
试试我的支持Rails 3.2的gemified版本:
#1
7
I made the following changes and it worked
我做了以下更改,但它确实有效
attachment_fu.rb
def temp_path
p = temp_paths.first
if p.is_a?(ActionDispatch::Http::UploadedFile) # Rails 3.0.3 compatability fix
p.tempfile.path
else
p.respond_to?(:path) ? p.path : p.to_s
end
end
I also changed returning filename.strip do |name|
to filename.strip.tap do |name|
我也改变了返回的filename.strip do | name | to filename.strip.tap do | name |
init.rb
def make_tmpname(basename, n)
ext = nil
n ||= 0
sprintf("%s%d-%d%s", basename.to_s.gsub(/\.\w+$/) { |s| ext = s; '' }, $$, n, ext)
end
I made a fork on github with this changes https://github.com/debprado/attachment_fu
我在github上用这个改变做了一个分支https://github.com/debprado/attachment_fu
#2
6
attachment_fu patches Tempfile.make_tmpname in attachment_fu/init.rb, and it doesn't work in 1.9.2: sprintf("%d",nil) fails, and in 1.8.7 the result of this expression is "0".
attachment_fu修补attachment_fu / init.rb中的Tempfile.make_tmpname,它在1.9.2中不起作用:sprintf(“%d”,nil)失败,而在1.8.7中,该表达式的结果为“0”。
The fix is to insert a line in init.rb from:
解决方法是在init.rb中插入一行:
sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)
to
n ||= 0
sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n, ext)
You can find some of the discussion here https://github.com/technoweenie/attachment_fu/issues/25
你可以在这里找到一些讨论https://github.com/technoweenie/attachment_fu/issues/25
Cheers!
#3
3
Try my gemified version that supports Rails 3.2:
试试我的支持Rails 3.2的gemified版本: