作为R-O-R的新手,我正在尝试在网络应用中添加文件上传功能。我正在使用rails 4,ruby 3.0并尝试使用carrierwave gem进行文件上传。我做了一个例子,工作正常。但是当我实现相同的代码时,我遇到了一个问题,而且无法确定我在哪里犯了错误。
示例我提到了学习http://richonrails.com/articles/allowing-file-uploads-with-carrierwave
创建网络应用视图:
<div class="well">
<%= form_for [:admin, @firmware_version], html: { multipart: true } do |f| %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= f.submit "Save", class: "btn btn-primary" %>
<% end %>
</div>
创建了一个上传的&#34; FirmwareUploader&#34;
class FirmwareUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"/home/user1/filedownloadtrial"
end
end
模型如下:
class FirmwareVersion < ActiveRecord::Base
attr_accessible :hw_supported, :upload_location, :version
mount_uploader :attachment, FirmwareUploader # Tells rails to use this uploader for this model.
end
我的模型没有列&#34;附件&#34;所以我创建了一个用于添加列的迁移&#34;附件&#34;作为字符串。我在尝试修复问题时创建了这个,但它没有帮助。
控制器的创建动作
def create
@firmware_versions = FirmwareVersion.new(params.require(:firmware_version).permit(:attachment))
if @firmware_versions.save
redirect_to firmware_versions_path, notice: "The firmware_versions has been uploaded."
else
render "new"
end
端
因此,当我选择要上传的文件时,&#34;保存&#34;单击,我收到以下错误:
TypeError in Admin::FirmwareVersionsController#create
no implicit conversion of Symbol into String
....
....
This error occurred while loading the following files:
firmware_version
以下是调用堆栈:
activesupport (3.2.13) lib/active_support/dependencies.rb:251:in `require'
activesupport (3.2.13) lib/active_support/dependencies.rb:251:in `block in require'
activesupport (3.2.13) lib/active_support/dependencies.rb:236:in `load_dependency'
activesupport (3.2.13) lib/active_support/dependencies.rb:251:in `require'
app/controllers/admin/firmware_versions_controller.rb:50:in `create'
actionpack (3.2.13) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.13) lib/active_support/callbacks.rb:469:in `_run__852872005__process_action__698653545__callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.13) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.13) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.13) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.13) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.13) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.13) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.13) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.13) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.13) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.13) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.13) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.13) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:36:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:612:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
actionpack (3.2.13) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.5) lib/rack/etag.rb:23:in `call'
我搜索过但无法弄清楚这里有什么问题。
答案 0 :(得分:0)
这个问题来自于控制器。 @ firmware_versions = FirmwareVersion.new(params.require(:firmware_version).permit(:attachment))
我把它改成了 @firmware_versions = FirmwareVersion.new @ firmware_versions.attachmant = params [:firmware_version] [:attachment]