TypeError(没有将nil隐式转换为String):

时间:2016-01-14 03:34:21

标签: ruby-on-rails ruby

日志表明我的一个控制器中的错误在此行中

    original_cloudinary = Cloudinary::Uploader.upload(params[:original_image],
                                     :public_id => params[:band_name].gsub(/[^0-9a-z]/i, '_')+"-"+params[:shirt_name].gsub(/[^0-9a-z]/i, '_')+"-"+"#{Time.now.to_i}",
                                     :discard_original_filename => true)

是否有导致此错误的原因?

这是足够的信息吗?如果您需要更多,请告诉我您的需求。任何帮助都会非常有帮助。我充其量只是一个黑客程序员,只是感觉我的方式。谢谢!

1 个答案:

答案 0 :(得分:0)

正如评论中所述,您很可能在params[:band_name]params[:shirt_name]中都是零 - 您永远不会保证会出现这些内容,并且您无法在其上调用字符串方法当他们没有时,你需要先检查它们。例如尝试这样的事情:

# do something better than raising an exception
raise "no band_name" if params[:band_name].blank?
raise "no shirt name" if params[:shirt_name].blank?

# better to put complicated things into variables rather than have a really long unreadable line
band_name = params[:band_name].gsub(/[^0-9a-z]/i, '_')
shirt_name = params[:shirt_name].gsub(/[^0-9a-z]/i, '_')
public_id = "#{band_name}-#{shirt_name}-#{Time.now.to_i}"

original_cloudinary = Cloudinary::Uploader.upload(params[:original_image],
                                 :public_id => public_id,
                                 :discard_original_filename => true)