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