我试图将图像文件上传到我的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
空了吗?&#39;为#
> 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时爆炸。我认为图像字段为零但是当我从服务器检查img_attr时,我注意到上传的文件存在且临时字段正确地指向本地副本。
当我深入挖掘时,我注意到我的themed_controller中提交的cover
无效。
MOPED: 127.0.0.1:27017 INSERT database=mangoweb_development collection=themed_campaigns
文档= [{&#34; _id&#34; = GT; BSON ::的ObjectId(&#39; 53ba605c5661731da8060000&#39), &#34; campaign_title&#34; =&gt;&#34;测试广告系列&#34;,&#34; campaign_description&#34; =&gt;&#34;测试 广告宣传&#34;,&#34; live_story_ids&#34; =&gt; [&#34; 53a9b3e5566173146e030000&#34;, &#34; 5343eafc5661731195030000&#34;,&#34; 5343eacc566173117f030000&#34;, &#34; 5343eadf5661731189030000&#34;],&#34; valid_from&#34; =&gt; 2014-07-07 00:00:00 UTC, &#34; valid_till&#34; =&gt; 2014-07-22 00:00:00 UTC, &#34;盖&#34; = GT;&#34;#&#34 ;, &#34; added_by_email&#34; = GT;&#34; XXXXXXXXX&#34 ;, &#34; campaign_sane_title&#34; =&gt;&#34;测试广告&#34;,&#34; updated_at&#34; =&gt; 2014-07-07 08:54:52 UTC,&#34; created_at&#34; =&gt; 2014-07-07 08:54:52 UTC}] flags = [] COMMAND database = mangoweb_development command = {:getlasterror =&gt; 1,:w =&gt; 1} 运行时间:11.9600ms #
我在这里做错了什么?为什么cover的值是上传的Object引用的String版本,而不是文件值,以及为什么Image没有被创建?
过去两天我一直在讨论这个问题,并查看了各种参考资料。我知道,我必须做一些非常愚蠢的事情,但我无法得到它。
请帮我解决这个问题。提前致谢。
答案 0 :(得分:1)
您可以使用表单实例对象轻松处理此
更改
form_for(@campaign, validate: true, :html => {:multipart => true}) do
到
form_for(@campaign, validate: true, :html => {:multipart => true}) do |f|
并在引用表单字段时使用f
%textarea.form-input{ name: "@campaign[campaign_description]", placeholder: "Campaign Description", required: "", value: ""}
应改为
f.text_area :campaign_description, placeholder: "Campaign Description", required: "", value: ""
和文件字段
f.file_field :cover
如果有效,请告诉我