图像的字段不以活动管理员形式显示

时间:2012-07-19 18:18:12

标签: ruby-on-rails ruby paperclip activeadmin formtastic

gem“formtastic”,“〜> 2.1.1” gem“activeadmin”,“〜> 0.4.2” 宝石“回形针”

照片的字段不会显示在活动管理员表单app / views / admin / products / _form.html.erb中,但是 app / views / products / _form.html.erb中的相同表单在产品的视图中正常工作

>应用/管理/ products.erb

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

应用程序/视图/管理/产品/ _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 %>

2 个答案:

答案 0 :(得分:6)

我找到了解决方案。

如果您想将paperclip与active_admin一起使用,则无法渲染外部表单,因为它无法在其中使用has_many关联。 我的解决方案:

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

答案 1 :(得分:1)

对于那些来这里寻找显示回形针上传图片的解决方案的人来说,主动管理编辑/新表格是。

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

这里的关键思想是使用提示属性。