Ruby on rails 使用嵌套的_形式gem跟踪不需要的重复
我不确定这里发生了什么。我已经检查了这里的所有内容,但是我得到了这个双文件上传字段: 即使我只使用一个字段,它仍然渲染两个图像Ruby on rails 使用嵌套的_形式gem跟踪不需要的重复,ruby-on-rails,nested-forms,Ruby On Rails,Nested Forms,我不确定这里发生了什么。我已经检查了这里的所有内容,但是我得到了这个双文件上传字段: 即使我只使用一个字段,它仍然渲染两个图像 \u form.html.erb: <%= nested_form_for @home, :html=>{:multipart => true } do |f| %> <% if @home.errors.any? %> <div id="error_explanation"> <h2&g
\u 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 %>
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
mount_uploader :file, FileUploader
end
class Home < ActiveRecord::Base
attr_accessible :body, :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments, :allow_destroy => true
end
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
home.rb
:
<%= 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 %>
class Attachment < ActiveRecord::Base
attr_accessible :description, :file
belongs_to :attachable, :polymorphic => true
mount_uploader :file, FileUploader
end
class Home < ActiveRecord::Base
attr_accessible :body, :attachments_attributes
has_many :attachments, :as => :attachable
accepts_nested_attributes_for :attachments, :allow_destroy => true
end
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
我该如何阻止这种情况发生
这方面的任何帮助都会很好,我已经看了好几个小时了 我的第一个猜测是,在进行此设置时创建了错误的数据。可能
Home
对象上以前的附件未正确显示,但仍存在于数据库中。如果创建新的主页并尝试添加附件,是否会出现相同的问题
块的字段\u正在为传递的每个附件创建一个按钮和每张照片的显示。我想这就是你看到复制品的原因。的字段\u是附件上的迭代器
,其中有一个重复的迭代器(用于@home.attachments中的附件
)