attachment_fu 업데이트시 유효성 검사 실패
-
18-09-2019 - |
문제
나는 프로젝트에서 오랫동안 첨부 파일 _fu를 사용해 왔으며 모든 것이 괜찮 았지만 이제는 프로젝트를 Rails 2.3.3까지 가져 오려고 할 때 나는 나에게 미치는 이상한 버그를 겪고 있습니다. 이 경우 로고 인 첨부 파일은 Create에서 올바르게 유효성을 유지하지만 업데이트시 유효성 검사에 실패하지는 않습니다. 나는 그것을 디버깅했고 그것은 Intial Validate에 실패하지만 컨트롤러에서 나의 구조에 잡힌 예외를 던지지 않는 것 같습니다. 내가 모든 것을 시도한 것 같지만 이것을 알아낼 수는 없습니다.
제어 장치:
# POST /tournaments
# POST /tournaments.xml
def create
# Build tournament
@tournament = Tournament.new(params[:tournament].merge(:user_id => current_user.id) )
# Save the uploaded attachments
params[:uploads].each do |upload|
@tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
end unless params[:uploads].nil?
# if supplied save an event logo
@logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}) unless params[:logo].nil? or params[:logo][:upload_data].blank?
@tournament.logo = @logo unless @logo.nil?
respond_to do |format|
begin
Tournament.transaction do
@tournament.logo.save! unless @tournament.logo.nil?
@tournament.save!
end
flash[:notice] = 'Tournament was successfully created.'
format.html { redirect_to tournament_url(@tournament) }
format.xml { head :created, :location => tournament_url(@tournament) }
rescue
flash[:notice] = 'Errors prevented your Tournament from being saved'
format.html { render :action => "new" }
format.xml { render :xml => @tournament.errors, :status => :unprocessable_entity }
end
end
end
# PUT /tournaments/1
# PUT /tournaments/1.xml
def update
@tournament = Tournament.find(params[:id])
@tournament.user_id = session[:orig_user_id]
respond_to do |format|
begin
Tournament.transaction do
# Update Logo if necessary
unless params[:logo][:upload_data].blank?
@tournament.logo.destroy unless @tournament.logo.nil?
@tournament.logo = Logo.new({:uploaded_data => params[:logo][:upload_data]}.merge(:user_id => current_user.id))
end
# Save any uploaded documents
params[:uploads].each do |upload|
@tournament.documents << Document.new({:uploaded_data => upload[:document]}.merge(:description => upload[:description]))
end unless params[:uploads].nil?
# Update Tournamnet Attributes
@tournament.attributes = params[:tournament]
# Save the Tournament
@tournament.save!
end
flash[:notice] = 'Tournament was successfully updated.'
format.html { redirect_to tournament_url(@tournament) }
format.xml { head :ok, :location => tournament_url(@tournament) }
rescue
flash[:notice] = 'Errors prevented your Tournament from being updated'
format.html { render :action => "edit" }
format.xml { render :xml => @tournament.errors, :status => :unprocessable_entity }
end
end
end
로고 모델 :
class Logo < Asset
validate_on_create :attachment_valid?
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 4.megabytes,
:resize_to => '810x150>',
:processor => :ImageScience,
:thumbnails => { :thumb => '270x50>' }
def attachment_valid?
content_type = attachment_options[:content_type]
unless content_type.nil? || content_type.include?(self.content_type)
errors.add(:upload_data, " * must be an image file (jpg, gif, or png)")
end
size = attachment_options[:size]
unless size.nil? || size.include?(self.size)
errors.add(:upload_data, "* image must be 4MB or less")
end
end
before_thumbnail_saved do |thumbnail|
record = thumbnail.parent
thumbnail.user_id = record.user_id
thumbnail.listing_id = record.listing_id
end
end
나는 다음을 실행하고있다
레일 2.3.3
image_science 1.2.0
감사합니다 -TIM
해결책
a : prever_save 콜백을 사용하여 객체를 테스트 할 수도 있습니다. 유효하지 않은 경우 예외를 제기하십시오.
다른 팁
추가 시도 :
validate_on_update :attachment_valid?
제휴하지 않습니다 StackOverflow