2013-07-01 6 views
1

종이 클립은 모든 종류의 파일 업로드를 허용하고 있으며 이해가되지 않습니다. 내 앱에서는 기본적으로 사용자가 등록 할 때 아바타를 업로드 할 필요는 없지만 등록 후에는 아바타를 업데이트 할 수 있습니다. 그리고 사용자는 아바타를 성공적으로 업데이트 할 수 있습니다. 이 모두가 잘 작동하지만, 검증이에서 발로되지 않은 User.rb에서 아래종이 클립 유효성 검사가 작동하지 않습니다.

검증 코드 :.

put 'updateavatar' => 'profile#updateavatar' 

이 내입니다 :

has_attached_file :avatar, :styles => { :profile => "150x150#"}, :default_url => 'missing_:style.png' 

validates_attachment :avatar, presence: true, 
      content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png'], :message => 'must be a PNG, JPG, or JPEG'}, 
      size: {less_than: 5.megabytes, :message => 'must be less than 5 megabytes'} 

을 내 경로에서 나는 이것을 가지고 양식 :

<%= form_for current_user, :html => { :multipart => true }, :url => {:action => 'updateavatar'} do |form| %> 
    <%= form.file_field :avatar %> 
    <%= form.submit "Upload", class: "btn uploadbtn" %> 
<% end %> 

왜 이것이 작동하지 않을지 모르겠습니까? 사용자가 프로필을 업데이트하면 문자 그대로 모든 종류의 파일을 업로드 할 수 있습니다.

def updateavatar 
    if params[:user][:password].blank? 
    params[:user].delete(:password) 
    params[:user].delete(:password_confirmation) 
    end 
    respond_to do |format| 
    if current_user.update_attribute(:avatar, params[:user][:avatar]) 
     flash[:notice] = 'successfully updated.' 
     format.html { redirect_to profile_index_path } 
    else 
     format.html { render action: "index" } 
    end 
    end 
end 
+0

별도의 유효성으로 validates_attachment_content_type 사용해보십시오. –

+0

유감스럽게도 유효성 검사는 여전히 건너 뛰고 있습니다. –

+0

http://stackoverflow.com/a/12686796/1251349 –

답변

3

update_attribute

# File vendor/rails/activerecord/lib/active_record/base.rb, line 2614 
2614:  def update_attribute(name, value) 
2615:   send(name.to_s + '=', value) 
2616:   save(false) 
2617:  end 

update_attributes

# File vendor/rails/activerecord/lib/active_record/base.rb, line 2621 
2621:  def update_attributes(attributes) 
2622:   self.attributes = attributes 
2623:   save 
2624:  end 

때문에,를 사용하여 내 프로필 컨트롤러에서

나는이 있습니다 210은 개체를 업데이트하지만 유효성 검사를 건너 뛰고 update_attributes은 유효성 검사를 통해 개체를 업데이트합니다.

는 컨트롤러가 있어야 같다 :

if current_user.update_attributes(:avatar, params[:user][:avatar]) ..... 
+0

감사합니다. 나는 그것을 알아 냈다. –

+0

감사를 건너 뛰기 때문에'update_attribute'를 사용할 때 조심하십시오. – SsouLlesS

0

current_user.update_attributes (: 아바타 => PARAMS [: 사용] : 아바타]) 고정 그것을

관련 문제