5

我正在用Paperclip 2.3.8運行Rails 3.0.3。我有兩個模型,稱它們爲「Post」和「Image」。圖像是一個包含Paperclip圖像文件的對象,用於動態添加圖像到Posts。圖片屬於帖子,帖子有很多圖片。銷燬方法沒有開火以銷燬複雜形式的嵌套資源

郵政型號:

class Post < ActiveRecord::Base 
has_many :images 
accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:image].blank? }, :allow_destroy => true 
end 

圖片型號:

class Image < ActiveRecord::Base 
belongs_to :post 
has_attached_file :image, 
     :styles => { 
     :thumb=> "100x100#", 
     :small => "300x300>" } 
end 

我在我想動態地添加和移除的圖像的形式。如果圖像是上傳的新圖像,我想顯示一個file_field;否則,如果圖像已經存在,我想顯示圖像和刪除鏈接。我跟隨瑞恩貝茨的嵌套表格Railscasts。動態添加圖像正在工作,但對_destory的調用不會觸發。該帖子包含「_destroy」=>「1」,但在HTTP Post事件之後,ID爲7的圖像仍然存在。下面是從HTTP後的摘錄:

"images_attributes"=>{ 
    "0"=>{"id"=>"7", "_destroy"=>"1"}, 
    "1"=>{"id"=>"8", "_destroy"=>"false"}, 
    "2"=>{"id"=>"9", "_destroy"=>"false"}, 
    "3"=>{"id"=>"10", "_destroy"=>"false"}} 

這裏是什麼形式的樣子:

<%= form_for @post, :html => { :multipart => true } do |f| %> 
<%= f.label :images, 'Images' %><br /> 
<div class="field"> 
    <%= f.fields_for :images do |s| %> 
    <%= render 'image_fields', :f => s %> 
    <% end%> 
</div> 
<div class="field"> 
    <%= link_to_add_fields "Add Image", f, :images %> 
</div> 
<div class="actions"> 
    <%= f.submit %> 
</div> 
<% end %> 

_image_fields:

<div class="fields"> 
<% if !f.object.new_record? %> 
    <%= image_tag f.object.image.url %> 
<% else %> 
    &nbsp; <%= f.file_field :image%> 
<% end %> 
<%= link_to_remove_fields "remove", f %> 
</div> 

JavaScript函數:

function remove_fields(link){ 
$(link).previous("input[type=hidden]").value = "1"; 
$(link).up(".fields").hide(); 
} 

function add_fields(link, association, content) { 
var new_id = new Date().getTime(); 
var regexp = new RegExp("new_" + association, "g") 
$(link).up().insert({ 
    before: content.replace(regexp, new_id) 
}); 
} 

助手功能:

def link_to_remove_fields(name, f) 
    f.hidden_field(:_destroy) + 
    link_to_function(name, "remove_fields(this)") 
end 

def link_to_add_fields(name, f, association) 
new_object = f.object.class.reflect_on_association(association).klass.new 
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder| 
    render(association.to_s.singularize + "_fields", :f => builder) 
end 
link_to_function(name, "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")") 
end 

正如我上面提到的,我可以添加圖像。單擊「添加圖像」鏈接將一個文件字段添加到表單中,我可以成功添加多個圖像。點擊圖片中的「刪除」鏈接會從視圖中刪除鏈接和圖片 - 但正如我所說的,它不會在發佈後事件中刪除。

謝謝你的高級;我感謝任何花時間幫助我找到解決方案的人。

編輯:

--Solution--

在崗控制器加入 「.includes(:圖片)」 在更新方法查找:

def update 
@post = Post.includes(:images).find(params[:id]) 

respond_to do |format| 
    if @post.update_attributes(params[:post]) 
    format.html { redirect_to(@post, :notice => 'Post was successfully updated.') } 
    format.xml { head :ok } 
    else 
    format.html { render :action => "edit" } 
    format.xml { render :xml => @post.errors, :status => :unprocessable_entity } 
    end 
end 
end 

回答

9

我已經有一個非常類似的問題。我可以通過刪除模型中的:reject_if子句來解決它。

accepts_nested_attributes_for :images, :allow_destroy => true 

一旦我刪除它,我可以刪除我的領域沒有問題。這個明顯的缺點是用戶現在可以添加一個空白/無效的圖像,模型不會拒絕它。

編輯:

看起來這實際上是使用Rails 3.0.4在問候的accepts_nested_attributes_for如何處理錯誤:allow_destroy和:reject_if。請參閱此處的答案以獲得另一種解決方法:Paperclip problem for accepts_nested_attributes_for and reject_if

+0

儘管如果我刪除了reject_if,這不是我的選擇。我能夠通過其他提供的鏈接找到答案。謝謝。 (編輯於上) – dmlogs 2011-02-24 18:22:34