1

我正在开发我的第一个 Rails 应用程序,并尝试在一个表单中设置多个图像上传。我创建了一个有很多参赛作品的比赛,参赛作品可以有很多图像。我有参赛作品,但我无法让表格的图片上传部分正常工作。我正在关注链接到载波 wiki 的本教程http://lucapette.com/rails/multiple-files-upload-with-carrierwave-and-nested_form/ 。

参赛作品模型

class ContestEntry < ActiveRecord::Base
  attr_accessible :body, :title, :contest_id, :entry_images_attributes
  validates :user_id, presence: true
  validates :title, presence: true, length: { maximum: 140 }
  validates :body, presence: true
  validates :contest_id, presence: true

  belongs_to :contest
  belongs_to :user

  has_many :entry_images, as: :imageable

  accepts_nested_attributes_for :entry_images
end

入口形象模型

class EntryImage < ActiveRecord::Base
  attr_accessible :alt, :image_path

  belongs_to :imageable, :polymorphic => true

  validates :image_path, presence: true

  mount_uploader :image, ImageUploader
end

新的报名表

<%= nested_form_for([:contest, @entry], :html => {:multipart => true}) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.hidden_field :contest_id %>
    <p>
        <%= f.label :title %><br />
        <%= f.text_field :title %>
    </p>

    <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
    </p>

    <%= f.fields_for :entry_images do |builder|  %>
        <p>
        <%= builder.label :alt %><br />
        <%= builder.text_field :alt %>
        </p>
        <p>
        <%= builder.label :image_path %><br />
        <%= builder.file_field :image_path %>
        </p>

    <% end %>
    <p><%= f.link_to_add "Add Image", :entry_images %></p>

    <%= f.submit "Enter", class: "btn btn-large btn-primary" %>
<% end %>
<%= javascript_include_tag :defaults,"nested_form" %>

出于某种原因,<%= f.fields_for :entry_images do |builder| 中没有显示任何内容 %> 块。它只是空白,也没有错误消息。如果我将它切换到 <%= f.fields_for :entry_image do |builder| %> 并切换到使用 form_for 而不是 nested_form_for 插件,所有字段都会显示,但是在提交时出现错误。

有任何想法吗?

谢谢,科里

4

0 回答 0