使用回形针的嵌套表格
我有一个名为posts的模型,它有很多附件。
附件模型使用回形针。
我创建了一个独立的模型来创建工作正常的附件,这是这里指示的视图(https://github.com/thoughtbot/paperclip):
@attachment, :html => { :multipart => true } do |form| %>
post中的嵌套表单如下所示:
prohibited this post from being saved:
@attachment, :html => { :multipart => true } do |at_form| %>
创建附件记录,但其为空。 该文件未上传。 同时,该post已成功创建……
有任何想法吗?
您缺少表单定义中的:multipart选项:
<% @attachment = @post.attachments.build %> <%= form_for @post, :html => { :multipart => true } do |f| %> <% if @post.errors.any? %> <%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:
<% @post.errors.full_messages.each do |msg| %> - <%= msg %>
<% end %>
<% end %> <%= f.label :name %>
<%= f.text_field :name %> <%= f.label :description %>
<%= f.text_area :description %> <%= f.fields_for :attachments, @attachment do |at_form| %> <%= at_form.file_field :pclip %> <% end %> <%= f.submit %> <% end %>
另外,你的@posts变量应该是@post(单个ActiveRecord实例而不是ActiveRecord实例数组)。