我已经看过所有其他类似的问题看起来很相似,但我找不到能帮到我的答案。
我正在努力升级之前写过的应用,并且正在努力让它的图像上传部分正常工作。代码的相关位如下:
photos_controller.rb:
def create
images = params[:an_image]
if !images.nil?
photos = []
images.each do |image|
photo = @trip.photos.build(:user => current_user, :trip => @trip, :image => image)
if photo.save
p 'saving ok'
photos << photo
else
p 'ERROR saving'
end
end
add_news('created_multiple', photos.first, {:photo_ids => photos.collect(&:id)})
flash[:notice] = "Your photos were updated successfully!"
redirect_to @trip
else
@photo.user = current_user
@photo.trip = @trip
if @photo.update_attributes(params[:photo]) || params[:photo][:an_image]
add_news('created', @photo)
redirect_to trip_photos_url(@photo.trip)
else
render :edit
end
end
end
photo.rb:
def an_image=(file)
self.trip.photos.create(:image => file)
end
attr_accessor :an_image
# Photo image
if Rails.env.test?
has_attached_file :image, :path => "/tmp/:attachment-:id-:style.:extension"
else
has_attached_file :image,
# 200 x 160 for news
:styles => {
:list => "270x200>",
:feed_item => "75x60>",
:map => "30x30#",
:homepage => "160x110#",
:normal => "300x500",
:tiny => "20x20#"
},
:processors => [:auto_orient, :thumbnail],
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => "saild-production",
:convert_options => {:map => '-bordercolor white -compose over -border 2'}
end
和photos / new.html.erb:
<div id="content-four">
<h1>Upload photos to <%= @trip.name %></h1>
<%= form_tag trip_photos_path(@trip), :multipart => true %>
<form id="post-photo" action="#">
<fieldset class="multiple-uploads">
<a href="#none" class="duplicate" rel=".file-upload">Add more files</a><br>
<%= file_field_tag :an_image, :class => 'file-upload' %>
<script type="text/javascript">
$('.duplicate').click(function() {
$('.file-upload:first').clone().appendTo($('.multiple-uploads'))
})
</script>
</fieldset>
<fieldset>
<input type="submit" class="submit button" value="Publish"/>
</fieldset>
</form>
</div>
但是当我尝试上传文件时,我不断收到此错误:
NoMethodError (undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007f971263b860>):
app/controllers/photos_controller.rb:33:in `create'
actionpack (3.0.7) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.7) lib/active_support/callbacks.rb:461:in `_run__1507672816926971504__process_action__674209566928286804__callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:410:in `_run_process_action_callbacks'
activesupport (3.0.7) lib/active_support/callbacks.rb:94:in `run_callbacks'
actionpack (3.0.7) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.7) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.7) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.7) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.7) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.7) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.7) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.7) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.7) lib/action_controller/metal.rb:178:in `block in action'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.7) lib/action_dispatch/routing/route_set.rb:493:in `call'
hoptoad_notifier (2.4.11) lib/hoptoad_notifier/rack.rb:27:in `call'
sass (3.4.12) lib/sass/plugin/rack.rb:54:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.8) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.7) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.7) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.7) lib/active_support/callbacks.rb:416:in `_run_call_callbacks'
actionpack (3.0.7) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.8) lib/rack/sendfile.rb:106:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
airbrake (4.1.0) lib/airbrake/rails/middleware.rb:13:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.7) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.8) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.7) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.8) lib/rack/lock.rb:13:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.8) lib/rack/lock.rb:13:in `call'
actionpack (3.0.7) lib/action_dispatch/middleware/static.rb:30:in `call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:16:in `_call'
airbrake (4.1.0) lib/airbrake/user_informer.rb:12:in `call'
hoptoad_notifier (2.4.11) lib/hoptoad_notifier/user_informer.rb:12:in `call'
railties (3.0.7) lib/rails/application.rb:168:in `call'
railties (3.0.7) lib/rails/application.rb:77:in `method_missing'
railties (3.0.7) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.8) lib/rack/content_length.rb:13:in `call'
rack (1.2.8) lib/rack/handler/webrick.rb:52:in `service'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/camillavk/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
我认为代码尝试做的是创建一个上传的图像数组然后.build(我从未遇到过 - &gt;我认为它就像.create?)单独地,但似乎并不想将上传的文件放入数组......
有谁知道如何解决这个问题?
(我正在运行Rails 3.0.7和Ruby 1.9.3)
答案 0 :(得分:1)
您的问题是params[:an_image]
是一个单独的对象,您在其上调用.each
就好像它是一个数组一样。
之前我没有尝试使用文件数据,但尝试将file_field_tag的name
属性更改为末尾有方括号,例如
file_field_tag "an_image[]"
如果在name属性的末尾有[]
,则具有相同name属性的所有输入中的值将一起收集到一个数组中。可能就像那样简单......另一方面,可能有一些关于如何构建多部分数据来阻止这种情况。
另一个提示是,如果必须处理可能是数组或单个对象的变量,则将其包装在数组中,然后在其上调用flatten
。例如
#turns foos into an array, whether it is a single object or an array already
foos = [foos].flatten
现在你可以foos.each
安全地知道它永远是一个数组。
另外 - tail
您的development.log文件,当您提交表单时,您可以查看params
中的内容,并将其与您的代码所期望的内容进行比较。