简体   繁体   中英

Can't create new record in Active Admin panel, Rails 4 and Devise

I have created ActiveAdmin controlpanel for my app. Problem is that I can't create new advertisement from there. Any other resource I can create without problems.

admin/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

Working form outside 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>

I have permitted all parameters inside 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]

Advertisement controller:

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

Advertisement model:

...
    has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures
...

When I try to create new advertisement I am redirected back to form without errors.

When I checked log files I got this info:

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"}

after that

  [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

I checked if ImageMagick is installed with :

/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

Then I compared log output from succesfull Advertisement creation and found that application doesn't try to save pictures. I was missing this output:

 [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

But why It wont work in AdminPanel but works in directly in application?

Any tips would be great. Thanks.

Maybe the problem is here

if params[:images]

i don't see the "images" param in the form, you used :pictures in your form declaration, and looking at the log, it seems it was returned as :pictures_attributes . So

@advertisement.pictures.create(image: image)

is never called?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM