你好,亲爱的程序员保存图像,导轨4 - 数据库
我试图发展与电子书的Web应用程序 “Praxiswissen - Ruby on Rails的”。我的问题是我想通过表单将图像保存到我的项目目录中。该数据库只是保存与节省时间的图像的名字:
def unique_and_proper_filename(filename)
Time.now.to_i.to_s + '_' + File.basename(filename)
end
我的问题是,我的照片不得到保存我提交表单后。我没有得到一些例外,这就是为什么我不知道我的问题在哪里。
控制器:
class PostsController < ApplicationController
require 'will_paginate'
def new
@post = Post.new
end
# information about saving the picture
def create
@post = Post.new(params[:post].permit(:title, :description, :date, :image_file, :thumbnail_file))
# Form isn't correctly filled message
if [email protected]?
flash.now[:notice] = "Bitte füllen Sie alle Felder aus und überprüfen Sie Ihre Angaben."
render(:action => :new)
# Files weren't saved message
elsif [email protected]_files
flash.now[:notice] = "Es trat ein Fehler beim Hochladen der Dateien auf."
render(:action => :new)
# Files saved correctly message
else
@post.save
flash[:notice] = "Dateien wurden hochgeladen und die Daten wurden gespeichert."
redirect_to(:action => :list)
end
end
# list action for listing my pictures
def list
@posts = Post.paginate(:page => params[:page], :order => "date DESC", :per_page => 15)
@post_pages = Post.paginate(:page => params[:page], :order => "date DESC", :per_page => 15)
end
end
HTML表单:
<h2>Neues Foto anlegen</h2>
<%= form_tag({:action => :create}, :multipart => true) %>
<h3>Bilddaten</h3>
<p>
Titel<br/>
<%= text_field(:post, :title) %>
</p>
<p>
Beschreibungen<br/>
<%= text_field(:post, :description) %>
</p>
<p>
Datum und Uhrzeit<br/>
<%= datetime_select(:post, :date, :order => [:day, :month, :year, :hour]) %>
</p>
<p>
<h3>Datei-Upload</h3>
<p>
Bilddatei:<br/>
<%= file_field(:post, :image_file) %>
</p>
<p>
Thumbnail:<br/>
<%= file_field(:post, :thumbnail_file) %>
</p>
<%= submit_tag("Speichern") %>
</p>
</form>
型号:
class Post < ActiveRecord::Base
validates_presence_of(:title, :description, :date, :image, :thumbnail)
I18n.enforce_available_locales = false
def image_file= (fileobj)
if fileobj.size > 0
@image_file = fileobj
self.image = unique_and_proper_filename(fileobj.original_filename)
end
end
def thumbnail_file= (fileobj)
if fileobj.size > 0
@thumbnail_file = fileobj
self.thumbnail = unique_and_proper_filename(fileobj.original_filename)
end
end
def save_files
# Bilddatei save
if !save_uploaded_file(@image_file, IMAGE_DIR, self.image)
return false
end
# Thumbnail save
if !save_uploaded_file(@thumbnail_file, THUMBNAIL_DIR, self.thumbnail)
return false
end
end
private
def unique_and_proper_filename(filename)
Time.now.to_i.to_s + "_" + File.basename(filename)
end
private
def save_uploaded_file(fileobj, filepath, filename)
# Complete Path
complete_path = Rails.root + "/public/" + filepath
# if neccessary, create directory
FileUtils.mkdir_p(complete_path) unless File.exists?(complete_path)
# save data
begin
f = File.open(complete_path + "/" + filename, "wb")
f.write(fileobj.read)
rescue
return false
ensure
f.close unless f.nil?
end
end
end
我只获得了有去一些错误保存文件的消息时,我正确填写表格,但它应该返回一条消息,说我的文件已保存。
我很抱歉,我的问题很长,但我真的不知道我的问题在哪里......如果需要更多信息或代码,我会尽快添加它。
非常感谢您提前!
你的日志文件(或服务器输出)会告诉你表单中的参数,以及出错的地方和发生的地方。诊断这样的问题时,始终是您首先着手的地方。 –