使用回形针嵌套表单
[英] Nested form using paperclip
本文介绍了使用回形针嵌套表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为 posts 的模型,它有很多附件.
附件模型使用回形针.
我制作了一个用于创建附件的独立模型,效果很好,这是此处指示的视图 (https://github.com/thoughtbot/paperclip):
<% form_for :attachment, @attachment, :url =>@attachment, :html =>{ :multipart =>真 } 做 |形式|%><%= form.file_field :pclip %><%=form.submit %><%结束%>
帖子中的嵌套表单如下所示:
<% @attachment = @posts.attachments.build %><%= form_for(@posts) 做 |f|%><% 如果@posts.errors.any?%><div id="error_explanation"><h2><%=pluralize(@posts.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @posts.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>
<div class="field"><%= f.label :description %><br/><%= f.text_area :描述%>
<div class="field"><%= f.fields_for :attachments, @attachment, :url =>@attachment, :html =>{ :multipart =>真 } 做 |at_form|%><%= at_form.file_field :pclip %><%结束%>
<div class="actions"><%= f.submit %>
<%结束%>
附件记录已创建,但为空.文件未上传.同时,帖子创建成功...
有什么想法吗?
解决方案
您的表单定义中缺少 :multipart 选项:
<% @attachment = @post.attachments.build %><%= form_for @post, :html =>{ :multipart =>真 } 做 |f|%><% 如果@post.errors.any?%><div id="error_explanation"><h2><%=pluralize(@post.errors.count, "error") %>禁止保存此帖子:</h2><ul><% @post.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>
<%结束%><div class="field"><%= f.label :name %><br/><%= f.text_field :name %>
<div class="field"><%= f.label :description %><br/><%= f.text_area :描述%>
<div class="field"><%= f.fields_for :attachments, @attachment do |at_form|%><%= at_form.file_field :pclip %><%结束%>
<div class="actions"><%= f.submit %>
<%结束%>
此外,您的@posts 变量应该是@post(单个 ActiveRecord 实例而不是 ActiveRecord 实例的数组).
I have a model called posts, and it has many attachments.
The attachments model is using paperclip.
I made a standalone model for creating attachments which works just fine, this is the view as instructed here (https://github.com/thoughtbot/paperclip):
<% form_for :attachment, @attachment, :url => @attachment, :html => { :multipart => true } do |form| %>
<%= form.file_field :pclip %>
<%= form.submit %>
<% end %>
The nested form in posts looks like this:
<% @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 %>
An attachment record is created, but its empty. The file is not uploaded. The post meanwhile, is successfully created...
Any ideas?
解决方案
You are missing the :multipart option in your form definition:
<% @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 %>
Also, your @posts variable should really be @post (single ActiveRecord instance as opposed to an array of ActiveRecord instances).
这篇关于使用回形针嵌套表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文