2

我有一个 Gallery 模型和一个 Images 模型,其中 Gallery has_and_belongs_to_many Images。

现在,通过图像表单上传新图像,并使用复选框从画廊表单中选择画廊的图像。我想保留现有图像的现有复选框选择方法,但也能够从图库表单中上传新图像(并同时创建新图像和图库之间的关联)。

这是我的画廊表格:

<%= semantic_form_for [:admin, @gallery] do |g| %>
  <%= g.inputs "Details" do %>
    <%= g.input :title %>
    <%= g.input :images, :as => :check_boxes, :label_method => Proc.new { |image| image_tag(image.thumb_path, :alt => "") + content_tag("h3", image.title)  } %>
  <% end %>
  <%= g.inputs "Images" do %>
    <% g.has_many :images do |i| %>
      <%= i.input :title %>
      <%= i.input :asset, :as => :file %>
    <% end %>
  <% end %>
  <%= g.buttons %>
<% end %>

我在浏览表单时看到以下错误:

undefined method `has_many' for #<Formtastic::SemanticFormBuilder:0xb410d4c>

我仍在学习 Rails,而且我对 ActiveAdmin 完全陌生,所以我可能在这里遗漏了一些明显的东西。如果有帮助,我很乐意提供更多背景信息。

感谢您提供的任何帮助!

4

1 回答 1

1

假设您已经设置了accepts_nested_attributes ...

首先创建一个空白图像 @gallery.images.build ,然后为新图像构建输入字段

<% g.inputs :for => :images do |image| %>
  <% if image.new_record? %>
        <%= image.input :title %>
        <%= image.input :asset, :as => :file %>
  <% end %>
<% end %>
于 2011-10-12T20:19:30.437 回答