本文介绍了图像的字段不会以活动管理员形式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

字段未以活动管理表单app / views / admin / products / _form.html.erb显示,但是
在app / views / products / _form.html.erb中的相同表单有效在产品视图中正确显示

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


ActiveAdmin.register产品操作

形式:partial => 表单

结尾

    <%= 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 %>


推荐答案

我找到了解决方案。

如果要在active_admin中使用回形针,则无法渲染外部表单,因为它无法使用has_many关联。
我的解决方案:

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

这篇关于图像的字段不会以活动管理员形式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:23