English 中文(简体)
File upload with Activeadmin Rails using paperclip
原标题:

I use Active admin as my rails application backend. I want to make a file upload. How can I accomplish this functionality?

最佳回答

I found a way to use Paperclip with Active Admin.

I added this code in my model "Event" :

has_attached_file :map, :styles => { :medium => "238x238>", 
                                   :thumb => "100x100>"
                                 }

And i did this for my admin model :

ActiveAdmin.register Event do
 form :html => { :enctype => "multipart/form-data" } do |f|
   f.inputs "Details" do
    f.input :continent
    f.input :event_type
    f.input :name
    f.input :title
    f.input :content
    f.input :date_start, :as => :date
    f.input :date_end, :as => :date
    f.input :place
    f.input :map, :as => :file
    f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url(:medium))
    f.input :userfull_info
    f.input :price
    f.input :phone, :as => :phone
    f.input :website, :as => :url
  end
  f.buttons
 end
end

To use it on the index page, you have to use :

column "Image" do |event|
    link_to(image_tag(event.image.url(:thumb), :height =>  100 ), admin_event_path(event))
  end
  default_actions
end
问题回答

Got it worked for Rails 4.1 and Paperclip 4.1:

Model

class Hotel < ActiveRecord::Base

has_attached_file :thumbnail, :styles => { :medium =>     "300x300#", :thumb => "200x200#" }
validates_attachment :thumbnail, content_type: { content_type:     ["image/jpg", "image/jpeg", "image/png"] }

end

Admin Model

ActiveAdmin.register Hotel do

permit_params :name, :description, :price, :thumbnail

form do |f|
  f.inputs "Project Details" do
    f.input :name
    f.input :thumbnail, :required => false, :as => :file
    # Will preview the image when the object is edited
  end
  f.actions
 end

show do |ad|
  attributes_table do
    row :name
    row :thumbnail do
      image_tag(ad.thumbnail.url(:thumb))
    end
    # Will display the image on show object page
  end
 end
end

I m using the rails 3.0.1 and the following code

f.input :image, :hint => "current image: #{f.template.image_tag(f.object.image.url(:thumb))}" 

return a string. After search a solution, i found it.

f.input :image, :hint => f.template.image_tag(f.object.image.url(:thumb))

Send direct the object, will return a image to the html

In latest Version of ActiveAdmin & Rails 6 for displaying the file field we need to use the below code

ActiveAdmin.register Project do
  permit_params :name, :uploads
  

  form multipart: true do |f|
    f.inputs "Project Details" do
      f.input :name
      f.input :uploads, as: :file, required: false
    end
    f.actions
  end

end

In some old version of AA following code also worked.

f.input :uploads, required: false





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签