I'm using ActiveAdmin to create 'slides' which are actually images that stored as binaries in database:

class CreateSlides < ActiveRecord::Migration
  def change
    create_table :slides do |t|
      t.binary :image
      t.timestamps
    end
  end
end

And my admin script is pretty straightforward too:

ActiveAdmin.register Slide do
  permit_params :image
  form do |f|
    f.inputs 'Slide details' do
      f.input :image, :as => :file
    end
    f.actions
  end 
end

So when I press 'Create slide' button, I get this exception:

wrong argument type ActionDispatch::Http::UploadedFile (expected String)

{"utf8"=>"✓",
 "authenticity_token"=>"A/Ux8rduVlaQQrimOV0qWFAhXR8ATJnfKupp03RVXmg=",
 "slide"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f1bb9515eb0 @tempfile=#<Tempfile:/tmp/RackMultipart20140507-20302-1yd7q93>,
 @original_filename="info.jpg",
 @content_type="image/jpeg",
 @headers="Content-Disposition: form-data; name=\"slide[image]\"; filename=\"info.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 },
 "commit"=>"Create Slide"}

I started use Rails again yesterday but I always was a newbe in it. I know a little about 'strong parameters' and I suppose that UploadFile is a valid type for them. So it's strange why do I have this error. I use Rails 4.1 and Active Admin 0.6.3.

有帮助吗?

解决方案

The problem there is not "strong parameters" - or you get a "forbidden..." error. The problem is that Rails broke the save-blob-to-db functionality, at least with regard to how it worked in Rails 3. This upgrade to 4 ...ugh!

I gave a workaround here: Trouble with uploading file after upgrating rails from 3.2 to 4

You will have to "fix" this in your controller, by taking what your form returns - I am guessing "params[:image]" - and adding the ".read" to it like this:

@slide.image = params[:image].read
@slide.save

That assumes you are using "@slide" for your model-object instance. If your code above is trimmed down, you get the added joy of isolating, removing, and saving that one param separately, as you cannot save it with the others, anymore.

See here for the methods why: http://api.rubyonrails.org/classes/ActionDispatch/Http/UploadedFile.html

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top