我對Rails中的嵌套窗體非常陌生,所以我相信我可能在這裏留下了一些東西。我正在遵循回形針教程,其中article
有很多assets
(回形針附件)Rails 4嵌套窗體不能生成多個
我堅持的部分是使用article
控制器創建多個文件上載字段。
您將在形式看到部分,我已經添加接近底部的資產模型:
<%= form_for(@article) do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<%= f.fields_for :asset do |asset| %>
<%= asset.file_field :image %>
<% end %>
<div class="actions">
<%= f.submit %>
<% end %>
而且在文章控制器,我加入了我認爲將建立形式5倍的那部分:
def new
@article = Article.new
5.times {@article.assets.build}
end
良好的措施,這裏是文章和資產型號:
文章
個class Article < ActiveRecord::Base
has_many :assets
end
資產
class Asset < ActiveRecord::Base
belongs_to :article
has_attached_file :image,
:style => {
:thumb => '150x150#',
:medium => '300x300>',
:large => '600x600>'
}
end
我缺少什麼?
釘了它。謝謝 – drewwyatt