我不确定这里发生了什么。我在这里检查了所有内容,但我得到了这个双文件上传字段:
即使我只使用一个字段,它仍会呈现两个图像。
_form.html.erb
:
<%= nested_form_for @home, :html=>{:multipart => true } do |f| %>
<% if @home.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@home.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% @home.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.fields_for :attachments do |attachment_form| %>
<%= attachment_form.file_field :file %>
<br /><br />
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% for attachment in @home.attachments %>
<div class="show_image">
<%= image_tag attachment.file_url(:thumb).to_s %>
</div>
<% end %>
<% end %>
<br /><br />
<%= f.link_to_add "Add attachmnt", :attachments %>
</p>
<br /><br />
<p><%= f.submit %></p>
<br /><br />
<% end %>
型号:
attachment.rb
:
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
mount_uploader :file, FileUploader
end
home.rb
:
class Home < ActiveRecord::Base
attr_accessible :body, :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments, :allow_destroy => true
end
homes_controller.rb
:
class HomesController < ApplicationController
# GET /homes
# GET /homes.json
def index
@homes = Home.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @homes }
end
end
# GET /homes/1
# GET /homes/1.json
def show
@home = Home.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @home }
end
end
# GET /homes/new
# GET /homes/new.json
def new
@home = Home.new
respond_to do |format|
format.html # new.html.erb
format.json { render :json => @home }
end
end
# GET /homes/1/edit
def edit
@home = Home.find(params[:id])
end
# POST /homes
# POST /homes.json
def create
@home = Home.new(params[:home])
respond_to do |format|
if @home.save
format.html { redirect_to @home, :notice => 'Home was successfully created.' }
format.json { render :json => @home, :status => :created, :location => @home }
else
format.html { render :action => "new" }
format.json { render :json => @home.errors, :status => :unprocessable_entity }
end
end
end
# PUT /homes/1
# PUT /homes/1.json
def update
@home = Home.find(params[:id])
respond_to do |format|
if @home.update_attributes(params[:home])
format.html { redirect_to @home, :notice => 'Home was successfully updated.' }
format.json { head :no_content }
else
format.html { render :action => "edit" }
format.json { render :json => @home.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /homes/1
# DELETE /homes/1.json
def destroy
@home = Home.find(params[:id])
@home.destroy
respond_to do |format|
format.html { redirect_to homes_url }
format.json { head :no_content }
end
end
end
如何阻止这种情况发生?
对此的任何帮助都会很棒,我已经看了好几个小时了!
答案 0 :(得分:0)
我的第一个猜测是在设置这个设置时创建的错误数据。也许Home
对象上的先前附件未正确显示,但仍存在于数据库中。如果您创建新的Home
并尝试添加附件,会发生同样的问题吗?
fields_for
块正在为每个传递的attachment
创建一个按钮和每张照片的显示。我认为这就是为什么你会看到重复的原因。 fields_for
是attachments
上的迭代器,其中有一个重复的迭代器(for attachment in @home.attachments
)。