fields for images don't displays in active admin form

强颜欢笑 提交于 2019-12-07 14:41:12

问题


gem "formtastic", "~> 2.1.1" gem "activeadmin", "~> 0.4.2" gem "paperclip"

fields for photos don't displays in active admin form app/views/admin/products/_form.html.erb ,but the same form in app/views/products/_form.html.erb works correctly in product's views

> app/admin/products.erb

ActiveAdmin.register Product do
form :partial => "form"
end

app/views/admin/products/_form.html.erb

    <%= semantic_form_for  [:admin , @product ], :html => { :multipart => true } do |f| %>
    <%= f.semantic_errors :name , :price , :description, :category_id %>

    <%= f.inputs :new_product do%>
        <%= f.input :name %>
        <%= f.input :price %>
        <%= f.input :description %>
        <%= f.input :category_id , :as => :select , :collection => Hash[Category.all.map{|c| [c.name, c.id]}] %>
    <% end %>    

  <%= f.inputs "Product images" do %>
     <%= f.fields_for :prod_images do |p| %>

         <%= p.input :photo, :as => :file, :label => "Image",:hint => p.template.image_tag(p.object.photo.url(:thumb)) %>

         <%= p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image' ,:hint => p.object.new_record? ? p.template.image_tag(p.object.photo.url(:thumb)) : p.template.image_tag(p.object.photo.url(:thumb)) %>

     <%end%> 
  <% end %>      

  <%= f.actions do %>
     <%= f.action :submit , :as => :button %>
  <% end %>

<% end %>

回答1:


I found the solution.

If you want to use paperclip with active_admin you can't render outer form, becouse it's not able to use has_many association in it. My solution:

ActiveAdmin.register Product do
  form :html => { :multipart=>true } do |f|
    f.inputs :new_product  do
      f.input :name
      f.input :price
      f.input :category
      f.input :description

      f.has_many :prod_images  do |p|
        p.input :photo, :as => :file, :label => "Image",:hint => p.template.image_tag(p.object.photo.url(:thumb)) 
        p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
      end
    end

    f.buttons  
  end
end



回答2:


For people coming here to find a solution for displaying paperclip uploaded images in active admin edit/new form is.

ActiveAdmin.register Product do

  form :html => { :enctype => "multipart/form-data" } do |f|
    f.inputs do
      f.input :title
    end

    f.inputs for: :prod_images do |product_image|
      if product_image.object.new_record?
        product_image.input :image
      else
        product_image.input :image, as: :file, hint: product_image.template.image_tag(product_image.object.image.url(:thumb))
      end
    end

    f.buttons
  end

Key idea here is to use hint attribute.



来源:https://stackoverflow.com/questions/11566943/fields-for-images-dont-displays-in-active-admin-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!