我正在尝试将图像文件上传到我的 Rails 服务器,但我一直碰壁。
这是我的themed_controller/new
表格
= form_for(@campaign, validate: true, :html => {:multipart => true}) do
.form-group
%label.col-sm-3.control-label
Campaign Name
.col-sm-6
%input#title.form-input{ name: "@campaign[campaign_title]", placeholder: "Campaign Title", required: "", type: "text", value: ""}/
.form-group
%label.col-sm-3.control-label
Campaign Description
.col-sm-6
%textarea.form-input{ name: "@campaign[campaign_description]", placeholder: "Campaign Description", required: "", value: ""}
.form-group
%label.col-sm-3.control-label
Pick stories
.col-sm-6
%select#themed_campaign_live_story_ids{name: "@campaign[live_story_ids][]", :multiple => true, :required=> true, value: []}
-# = f.select 'live_story_ids[]', [], :multiple => true
#date-range
.form-group#valid-from
%label.col-sm-3.control-label
Campaign runs from
.col-sm-6
%input.form-input#themed_campaign_valid_from{ name: "@campaign[valid_from]",:class => 'jquery-ui-date valid-from', :data => {:provide =>"datepicker", :behaviour => "datepicker"}, required: "", type: "text", value: ""}/
.form-group#valid-till
%label.col-sm-3.control-label
Campaign runs till
.col-sm-6
%input.form-input#themed_campaign_valid_till{ name: "@campaign[valid_till]",:class => 'jquery-ui-date valid-till', :data => {:provide =>"datepicker", :behaviour => "datepicker"}, required: "", type: "text", value: ""}/
.form-group#valid-till
%label.col-sm-3.control-label
Upload cover (1280x350)
.col-sm-6
-#upload-image.btn.btn-medium.btn-green{ :data => {:url => "/campaign/upload"} }
#upload-image
%input#image_media{name: "@campaign[cover]", type: "file"}/
.form-group
.col-sm-offset-3.col-sm-6
%button.form-button.btn-large.btn-green.btn#campaign-submit{type: "submit"}
Set up Campaign
这是我的创建方法
def create
campaign = params[:@campaign]
campaign['added_by_email'] = current_user.email
@campaign = ThemedCampaign.create(campaign)
if @campaign.save
img_attr = params[:@campaign]['cover']
image = Campaign.new img_attr
@campaign.cover = image
Resque.enqueue(ImageQueue,image.id)
#render :json => image.to_jq_upload.to_json
end
redirect_to '/admin/themecampaign'
end
我为此创建了一个单独的上传器,
# encoding: utf-8
class CampaignUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
before :cache, :save_original_filename
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg png)
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/campaigns/#{model.id}/"
end
end
这是我的竞选模式,其中包含图像
class Campaign
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Paranoia
include Rails.application.routes.url_helpers
mount_uploader :cover, CampaignUploader
end
现在,当我上传表格时,我一直是undefined method
空的?为了 #
> NoMethodError - undefined method `empty?' for
> #<ActionDispatch::Http::UploadedFile:0x00000109968c50>: () Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-bc72426681d5/lib/mongoid/attributes/processing.rb:21:in
> `process_attributes' ()
> Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-bc72426681d5/lib/mongoid/document.rb:111:in `block in initialize' ()
> Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-bc72426681d5/lib/mongoid/threaded/lifecycle.rb:84:in
> `_building' ()
> Users/skmvasu/.rvm/gems/ruby-2.0.0-p451/bundler/gems/mongoid-bc72426681d5/lib/mongoid/document.rb:105:in `initialize' actionpack (4.0.2)
> lib/action_dispatch/routing/url_for.rb:104:in `initialize'
> app/controllers/themed_campaign_controller.rb:57:in `new'
> app/controllers/themed_campaign_controller.rb:57:in `create'
从本质上讲,这在尝试使用图像属性调用 Campaign.new 时会爆炸。我以为 image 字段是 nil 但是当我从服务器检查 img_attr 时,我注意到上传的文件存在并且 temp 字段正确地指向本地副本。
当我深入挖掘时,我注意到cover
我的主题控制器中的文件无效。
MOPED: 127.0.0.1:27017 INSERT database=mangoweb_development collection=themed_campaigns
文档=[{"_id"=>BSON::ObjectId('53ba605c5661731da8060000'), "campaign_title"=>"测试活动", "campaign_description"=>"测试活动描述", "live_story_ids"=>["53a9b3e5566173146e030000", "5343eafc5661731195030000", "5343eacc566173117f030000", "5343eadf5661731189030000"], "valid_from"=>2014-07-07 00:00:00 UTC, "valid_till"=>20014-07-220" =>"#", " added_by_email"=>"xxxxxxxxx", "campaign_sane_title"=>"Test-campaign", "updated_at"=>2014-07-07 08:54:52 UTC, "created_at"=>2014- 07-07 08:54:52 UTC}] flags=[] COMMAND database=mangoweb_development command={:getlasterror=>1, :w=>1} 运行时间:11.9600ms #
我在这里做错了什么?为什么封面的值是上传的对象引用的字符串版本,而不是文件值,为什么没有创建图像?
在过去的两天里,我一直在讨论这个问题,并查看了各种参考资料。我知道,我一定在做一些非常愚蠢的事情,但我无法得到它。
请帮我解决这个问题。提前致谢。