1

ねえ、これは本当に私を悩ませています。簡単な画像のアップロードで Carrierwave をアプリに実装しました。画像が正しいファイルタイプであることを検証しようとしていますが、奇妙な結果がいくつか得られます。関連するコードは次のとおりです。

class Art < ActiveRecord::Base
belongs_to :collection
attr_accessible :image, :collection_id, :remote_image_url
mount_uploader :image, ImageUploader
validates_integrity_of :image
validates_presence_of :image
validates_processing_of :image
end

class ImageUploader < CarrierWave::Uploader::Base

include CarrierWave::MiniMagick

storage :file

def store_dir
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def extension_white_list
  %w(jpg jpeg gif)
end

class ArtsController < ApplicationController

def new
  @art = Art.new(:collection_id => params[:collection_id])
end

def edit
  @art = Art.find(params[:id])
end

def create
  @art = Art.new(params[:art])
    if @art.save
      flash[:notice] = "Successfully created art."
  redirect_to @art.collection
    else
      flash[:error] = @art.errors
  render :action => :new
   end
end

def update
  @art = Art.find(params[:id])

    if @art.update_attributes(params[:art])
  flash[:notice] = "Successfully updated art."
  redirect_to @art.collection
    else
  flash[:error] = @art.errors
  render :action => :edit
  end
end

def destroy
  @art = Art.find(params[:id])
  @art.destroy
flash[:notice] = "Successfully destroyed art."
redirect_to @art.collection
  end
end

en:
  errors:
   messages:
     carrierwave_processing_error: "Cannot resize image."
     carrierwave_integrity_error: "Not an image."
     carrierwave_download_error: "Couldn't download image."
     extension_white_list_error: "NOT ALLOWED"
     extension_black_list_error: "You are not allowed to upload %{extension} files, 
     prohibited types: %{prohibited_types}"

そのため、png ファイル形式で新しいアート作品を作成すると、画像を空白にできないというエラーが発生します。これはvalidates_presence_ofがトリガーであることは理解していますが、正しい形式ではないものを提供しています。validates_presence_of を削除すると、正しくないファイル形式は表示されませんが、作成されます。また、i18n エラー メッセージを en.yml ファイルに追加しました。私はレールに少し慣れていないので、各アート作品を作成する際にコントローラーに問題があると思いますが、何が間違っているのかわかりません。

4

0 に答える 0