使用Paperclip远程图像上传进行无限递归

时间:2013-02-23 00:29:39

标签: ruby-on-rails ruby-on-rails-3 paperclip infinite open-uri

在Trevor Turk的博客文章"Easy Upload via URL with Paperclip"中成功实施了几次代码后,在最新的迭代中,我的应用程序给了我以下错误:

Started POST "/pins" for 127.0.0.1 at 2013-02-22 14:05:20 -0300
Processing by PinsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Q6kmHS7Co+j2Y4JYd+3YO0OmDHU/upK1ADvRbS8UAIc=", "pin"=>{"image_url"=>"http://media-cache-ec3.pinterest.com/550x/b4/b7/82/b4b782bf268cb52d201853886510e3a9.jpg", "description"=>"Test"}, "commit"=>"Create Pin"}
  [1m[36mUser Load (0.2ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
  [1m[35m (0.1ms)[0m  begin transaction
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-c7vstl'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-17pzds5'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1kh7v9y'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-ngtq1u'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-jzmkac'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1x4vpj5'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1iwdp4q'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-cvro9z'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1tn727x'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-q77ipe'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1e5wt1x'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-7xaoic'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-kzw7ab'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-m1frw1'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-aojdb8'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-kzn0w6'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-cj55fi'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-d3s1h0'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-15hmo2q'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-17apjuj'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-f97eej'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1t9ou1f'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1dfnz21'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-ozc0ac'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1uiz51r'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-57ahli'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-u96rmb'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-vt2ph3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-st55ih'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1phbyb3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-wdu3nu'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1iit16u'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-117n6l2'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-qr5380'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-tod0l9'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1u19f33'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1rta45x'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-6o0hug'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-14e2zh7'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1lnwdhm'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-9en3jt'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-hhb0b2'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-9b0ak8'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1yyvnl9'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-15ebo0r'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-kpavxm'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-qgrtq2'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1erqxe5'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1h23wb0'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1heb0b3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1oswpj8'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1y1z7di'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-89spzg'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-96op3f'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-4aizhy'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1s5nw68'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-k4jb2y'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1gjqddi'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1nzej79'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1yurmzq'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1yvoyxn'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-ec4b0b'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-12zau9q'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-va8ryp'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-11362zg'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-14d0lek'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-65t94r'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-2zqkrs'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-nzvtnc'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-mokk7l'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1v6n3k3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-7tvcwq'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-z4qieu'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-5let78'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1azr7s1'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-xfbbhj'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-qpuxih'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1oqv313'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1lw2iyw'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-sgsyl7'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-z3kc9d'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1csf869'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1615j19'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-gjlms9'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1t0vhjc'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1x2w35'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1wgmxox'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-ce9qo7'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1mxr8v0'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1y1716x'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-19vj2ky'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1s2yq7p'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-10xd3y3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-fohqek'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1p8i5y4'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-mx211u'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-nhtv6r'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1sdvwt9'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1nu8xde'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1yo03yt'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-pfu1cg'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-oqslme'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1s8wcup'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1x91fzc'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1b3zxzf'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1miary7'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-18ef2u'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-eo8odm'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1noqove'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-hl4yhz'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-mqjd16'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-10do8b2'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-n1dqn2'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1raiaq5'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1myor64'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1i2e0b3'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-hgtnym'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-lzuyqt'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-1cvazmm'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-shgn3h'
Command :: file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-a1n19w'
  [1m[36m (0.1ms)[0m  [1mrollback transaction[0m
Completed 500 Internal Server Error in 201007ms

Errno::EMFILE (Too many open files - file -b --mime '/var/folders/8z/sh44jtbs33163s5hsjxj6tsc0000gr/T/open-uri20130222-25293-a1n19w'):
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/models/pin.rb:27:in `download_remote_image'
  app/controllers/pins_controller.rb:48:in `block in create'
  app/controllers/pins_controller.rb:47:in `create'

看起来发生了某种无限递归,虽然我无法弄清楚在哪里。

以下是相关模型的样子:

require 'open-uri'

class Pin < ActiveRecord::Base
  belongs_to :user
  attr_accessible :description, :image, :image_url

  has_attached_file :image, styles: { medium: "320x240>"}

  validates :description, presence: true
  validates :user_id, presence: true
  validates_attachment :image, presence: true,
                                                         content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] },
                                                         size: { less_than: 5.megabytes }


  # Remote image downloading
  attr_accessor :image_url
  before_validation :download_remote_image, :if => :image_url_provided?

  private

    def image_url_provided?
      !self.image_url.blank?
    end

    def download_remote_image
      self.image = URI.parse(image_url)
      # self.image_remote_url = image_url
    end

    def do_download_remote_image
      io = open(URI.parse(image_url))
      def io.original_filename; base_uri.path.split('/').last; end
      io.original_filename.blank? ? nil : io
    rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
      print "An error occurred: ",$!, "\n"
    end
end

我的别针/ _form.html.erb

<%= simple_form_for(@pin, html: { class: "form-horizontal"}) do |f| %>
  <%= f.error_notification %>
  <%= f.full_error :image_file_size, class: "alert alert-error" %>
  <%= f.full_error :image_content_type, class: "alert alert-error" %>

  <%= f.input :image, label: "Upload an image" %>
  <%= f.input :image_url, label: false, placeholder: "http://" %>
  <%= f.input :description, as: :text, input_html: { rows: "3" } %>

  <div class="form-actions">
    <%= f.button :submit, class: "btn btn-primary" %>
  </div>
<% end %>

最后,我的Pins控制器很标准:

  def new
    @pin = current_user.pins.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @pin }
    end
  end

  def create
    @pin = current_user.pins.build(params[:pin])

    respond_to do |format|
      if @pin.save
        format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
        format.json { render json: @pin, status: :created, location: @pin }
      else
        format.html { render action: "new" }
        format.json { render json: @pin.errors, status: :unprocessable_entity }
      end
    end
  end

好像它一遍又一遍地运行download_remote_image。

1 个答案:

答案 0 :(得分:1)

在阅读'before_validation'上的rails文档之后,似乎如果你没有指定动作,即(before_validation:download_remote_image,:on =&gt;:create),那么它每次都会运行被调用的方法改变了。在这种情况下,调用before_validation方法,然后在方法中,通过'self.image ='更改对象。如果未指定'on create',则会在此阶段再次调用before_validation,从而重新启动download_remote_image方法。当它到达'self.image ='时,它再次调用它,依此类推。所以可以添加':on =&gt; :在验证之前创建'或者在通过诸如'return if self.image.present?'之类的语句启动方法之前检查图像对象是否已经存在。

我对rails非常陌生,所以这个想法可能存在缺陷,但这对我来说很有意义。