2013-02-13 2 views
4

다시 귀하의 도움이 필요합니다. 이제 Carrierwave에서 업로드 한 파일 (내 경우에는 이미지)을 삭제하는 방법을 이해해야합니다.반송파 파일 삭제

모델/attachment.rb :

class Attachment < ActiveRecord::Base 
    belongs_to :attachable, :polymorphic => true 
    attr_accessible :file, :file 
    mount_uploader :file, FileUploader 
end 

모델/post.rb :

class Post < ActiveRecord::Base 
    attr_accessible :content, :title, :attachments_attributes, :_destroy 
    has_many :attachments, :as => :attachable 
    accepts_nested_attributes_for :attachments 
end 

* 보기/게시물/_form.html.erb : *

<%= nested_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 id="field"> 
    <%= f.label :Nosaukums %>:<br /><br /> 
    <%= f.text_field :title %><br /><br /> 
    </div> 
    <div id="field"> 
    <%= f.label :Raksts %>:<br /><br /> 
    <%= f.text_area :content %><br /><br /> 
    </div> 

    <%= f.fields_for :attachments do |attachment| %> 
    <% if attachment.object.new_record? %> 
     <%= attachment.file_field :file %> 

    <% else %> 
     <%= image_tag(attachment.object.file.url) %> 
     <%= f.check_box :_destroy %> 
    <% end %> 
    <% end %> 


    <%= f.submit "Publicēt", :id => "button-link" %> 
<% end %> 

나는이 오류가 이전에 업로드 된 파일을 삭제하려고 : 나는 여러 파일 업로드뿐만 아니라 하나를 가지고 있기 때문에

unknown attribute: _destroy 

아마 문제가 있습니다.

답변

3

잘못된 모델에서 메소드를 호출하고 있습니다. 파일 마운트가 첨부 파일에 있습니다.

오류는 무엇이 잘못되었는지를 알려줍니다.

undefined method 'remove_file' for #<Post:0x471a320 

오류의 핵심은 첨부 파일 모델에서 호출해야 할 때 게시 메서드에서 호출되는 것입니다.

입력란의 체크 범위를 올바른 모델로 설정하려고 시도했을 수 있습니다.

<%= attachment.check_box :remove_file %> 
4

docs에 따르면이 체크 박스는 remove_file이어야합니다.

+0

네, 본 적이 있지만'remove_file'을 시도 할 때 :이 오류가 있습니다 :'정의되지 않은 메소드 'remove_file'for # ' – RydelHouse

+0

메소드를 호출하려고합니다. 'remove_file'? 문서에 따르면,이 메소드는'remove_file!'이라고 불립니다 (느낌표에주의하십시오). – Tomdarkness

+0

나는이 모든 일들도했지만, 오류가있다. 팁을위한 – RydelHouse

3

이 있어야한다 <퍼센트 = attachment.check_box : _destroy %> 그것은이의

9

아무도 나를 위해 일한,하지만 파고 후 나는 this post 건너 온 나를 위해 작동

그 정말 도움이되었습니다. 이 확인란을 선택하고 다음과 같은 오류거야 형태로 제출하는 경우,

<%= f.check_box :remove_image %> 

:

Can't mass-assign protected attributes: remove_image

을 기본적으로 ...

양식 ( F 는 양식 객체 어디)

을 모델의 attr_accessible 목록에 간단하게 추가하면 쉽게 해결할 수 있습니다. 결국 다음과 같이 보일 것입니다 :

class Background < ActiveRecord::Base 
    attr_accessible :image, :remove_image 
    belongs_to :user 
    mount_uploader :image, BackgroundUploader 
end 

제 경우에는 사용자의 배경 이미지입니다.희망이있어 :

+1

thx! 나는 그것을 추가하여 작동하게했다 : remove_image를 내 컨트롤러 params.required ('my_object'). permit (: remove_image) 메서드 – Toontje