我为我的应用创建了ActiveAdmin控制面板。 问题是我无法从那里创建新广告。我可以毫无问题地创建任何其他资源。
系统管理员/ advertisement.rb
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Content" do
f.input :name
f.input :email
f.input :country
f.input :phone_number
f.input :region
f.input :age
f.input :weight
f.input :description
f.input :description_ru
end
f.inputs 'Workhours' do
f.input :work1
f.input :work2
f.input :work3
f.input :work4
end
f.inputs 'Services' do
f.input :services, :as => :check_boxes
end
f.inputs 'Blacklist (Information: Both checkboxes need to be checked to blacklist user! )' do
f.input :in_blacklist
f.input :admin_confirmed
end
f.inputs "Image" do
f.has_many :pictures do |ff|
ff.input :image, as: :file
ff.input :_destroy, as: :boolean
## ff.image_tag :photo
end
end
f.inputs 'Change password' do
f.input :password
f.input :password_confirmation
end
f.actions
end
ActiveAdmin外的工作表单:
<%= form_for @advertisement,remote: true ,format: :json,:html => {:multipart => true, :class => "form-horizontal advertisement" ,"data-parsley-validate" => true} do |f| %>
<div class="column-img-main">
<%= file_field_tag "images[]", type: :file, multiple: false,:id=>"myfile", :required => true%>
<%= image_tag("camera.png")%>
</div>
<div class="column-img-secundar">
<%= file_field_tag "images[]", type: :file, multiple: false,:id=>"myfile2"%>
<%= image_tag("camera.png")%>
</div>
<div class="column-img-secundar">
<%= file_field_tag "images[]", type: :file, multiple: false,:id=>"myfile3"%>
<%= image_tag("camera.png")%>
</div>
<div class="column-img-secundar">
<%= file_field_tag "images[]", type: :file, multiple: false,:id=>"myfile4"%>
<%= image_tag("camera.png")%>
</div>
<button name="name" id="submitbutton" value="value" type="submit"><span>Pievienot</span></button>
我已经允许 admin / advertisement.rb
中的所有参数 permit_params :id, :password,:password_confirmation,:name, :work1,:paid, :work2, :check, :can_add_review,:work3, :work4,:smsidentifier,:smsidentifier_confirmation,:provaider, :expiration,:your_ip,:in_blacklist,:admin_confirmed,:country_id, :region_id,:user_id,:phone_number,:image,:terms_of_service,:region,:age,:height,:weight,:client_age,:description,:description_ru,:email, service_ids: [], hour_ids: [], pictures_attributes: [:image, :id, :_destroy], commit: [:locale]
广告控制器:
def create
@advertisement = Advertisement.new(advertisement_params.merge(service_ids: params[:service_ids], hour_ids: params[:hour_ids]))
if @advertisement.save
if params[:images]
params[:images].each { |image|
@advertisement.pictures.create(image: image)
}
respond_with(@advertisement)
end
else
@services = Service.all
@country = Country.all
@hours = Hour.all
@regions = Region.where("country_id = ?", Country.first.id)
render 'new'
end
end
广告模式:
...
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures
...
当我尝试创建新广告时,我被重定向回形式没有错误。
当我查看日志文件时,我收到了以下信息:
Processing by Admin::AdvertisementsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"j84kG6C7du0kM4UvUHS5IgCk/xiFRNRdE1jrsz1RybE=", "advertisement"=>{"name"=>"444", "email"=>"wasd@gmail.com", "country_id"=>"1", "phone_number"=>"25245245", "region_id"=>"19", "age"=>"18", "weight"=>"45", "description"=>"<p>4jhbrefwe</p>", "description_ru"=>"<p>fewf</p>", "work1"=>"12", "work2"=>"12", "work3"=>"12", "work4"=>"12", "service_ids"=>["", "3", "5", "7"], "in_blacklist"=>"0", "admin_confirmed"=>"0", "pictures_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x000000077cab80 @tempfile=#<Tempfile:/tmp/RackMultipart20150207-10727-aq67z0>, @original_filename="block-3-image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"advertisement[pictures_attributes][0][image]\"; filename=\"block-3-image.jpg\"\r\nContent-Type: image/jpeg\r\n">, "_destroy"=>"0"}}, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Advertisement", "locale"=>"ru"}
之后
[1m[36mAdminUser Load (0.2ms)[0m [1mSELECT `admin_users`.* FROM `admin_users` WHERE `admin_users`.`id` = 2 ORDER BY `admin_users`.`id` ASC LIMIT 1[0m
Unpermitted parameters: locale
[1m[35mService Load (0.3ms)[0m SELECT `services`.* FROM `services` WHERE `services`.`id` IN (3, 5, 7)
Command :: file -b --mime '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-1ernhy3.jpg'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]' 2>/dev/null
Command :: identify -format %m '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]'
Command :: convert '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]' -auto-orient -resize "447x479>" '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k3420150207-10727-1nteal9'
Command :: file -b --mime '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k3420150207-10727-1nteal9'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]' 2>/dev/null
Command :: identify -format %m '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]'
Command :: convert '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k34.jpg[0]' -auto-orient -resize "100x100>" '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k3420150207-10727-b0yix1'
Command :: file -b --mime '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-wc6k3420150207-10727-b0yix1'
[1m[36m (0.3ms)[0m [1mBEGIN[0m
[1m[35mAdvertisement Exists (0.3ms)[0m SELECT 1 AS one FROM `advertisements` WHERE `advertisements`.`email` = BINARY 'wasd@gmail.com' LIMIT 1
[1m[36mRegion Load (0.3ms)[0m [1mSELECT `regions`.* FROM `regions` WHERE `regions`.`id` = 19 LIMIT 1[0m
[1m[35mCACHE (0.0ms)[0m SELECT 1 AS one FROM `advertisements` WHERE `advertisements`.`email` = BINARY 'wasd@gmail.com' LIMIT 1
Command :: file -b --mime '/tmp/7a0f2c7d5a272c46ee81fb7c26d055be20150207-10727-1nq2jam.jpg'
[1m[36m (0.3ms)[0m [1mROLLBACK[0m
我检查了ImageMagick是否安装了:
/ usr / local / bin / convert -version
Version: ImageMagick 6.8.9-10 Q16 x86_64 2014-11-14 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: x zlib
然后我比较了成功广告创建的日志输出,发现应用程序不会尝试保存图片。我错过了这个输出:
[1m[36mSQL (0.3ms)[0m [1mINSERT INTO `pictures` (`advertisement_id`, `created_at`, `image_content_type`, `image_file_name`, `image_file_size`, `image_updated_at`, `updated_at`) VALUES (85, '2015-02-07 16:41:12', 'image/jpeg', 'person.jpg', 13168, '2015-02-07 16:41:12', '2015-02-07 16:41:12')[0m
[1m[35m (30.3ms)[0m COMMIT
但是为什么它不能在AdminPanel中工作,而是直接在应用程序中工作?
任何提示都会很棒。 感谢。
答案 0 :(得分:1)
也许问题就在这里
if params[:images]
我没有在表单中看到“images”参数,你在表单声明中使用:pictures
,并查看日志,它似乎返回为:pictures_attributes
。所以
@advertisement.pictures.create(image: image)
永远不会被召唤?