午後はみんな、
新しいファイルのアップロードの作成を処理するコントローラーがあります (これはタスクなので、ペーパークリップを使用できず、データベースに保存されているため、これらすべての欠点を知っており、不平を言うのを聞くことができます 笑) ただし、検証時にファイルの保存が失敗する (つまり、何もアップロードしようとしない) ため、新しいアップロード フォームへのリダイレクトは何もしていないようで、インデックス ページをレンダリングしようとします。renders、redirect_to(:back) などを使用してリダイレクトのさまざまなバリエーションを試しましたが、実際には何もしていないようです。
誰かに何かアイデアがあれば、それは大歓迎です。
コードはこちらです。
コントローラ
def create
beginning = Time.now
return if params[:attachment].blank?
@attachment = Attachment.new
@attachment.uploaded_file = params[:attachment]
@time = (Time.now - beginning)
if @attachment.save
flash[:success] = "File uploaded in #{@time} seconds"
redirect_to @attachment
else
flash[:notice] = "something went wrong"
redirect_to 'new
end
end
モデル
class Attachment < ActiveRecord::Base
has_many :anagrams, dependent: :destroy
attr_accessible :filename, :content_type, :data
validates_presence_of :filename, :data, :content_type
def uploaded_file=(incoming_file)
self.filename = incoming_file.original_filename
self.content_type = incoming_file.content_type
self.data = incoming_file.read
end
def filename=(new_filename)
write_attribute("filename", sanitize_filename(new_filename))
end
private
def sanitize_filename(filename)
just_filename = File.basename(filename)
just_filename.gsub(/[^\w\.\-]/, '_')
end
end
ルート.rb
resources :attachments, only: [:create, :new]
resources :anagrams, only: [:create, :new]
root to: "attachments#new"
誰かがもっとコードを見る必要がある場合は、ただ叫んでください、どうもありがとう