2014-03-07 49 views
2

你好,親愛的程序員保存圖像,導軌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&uuml;llen Sie alle Felder aus und &uuml;berpr&uuml;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 

我只獲得了有去一些錯誤保存文件的消息時,我正確填寫表格,但它應該返回一條消息,說我的文件已保存。

我很抱歉,我的問題很長,但我真的不知道我的問題在哪裏......如果需要更多信息或代碼,我會盡快添加它。

非常感謝您提前!

+0

你的日誌文件(或服務器輸出)會告訴你表單中的參數,以及出錯的地方和發生的地方。診斷這樣的問題時,始終是您首先着手的地方。 –

回答

3

我很幸運地告訴我,我發現我的問題。我在我的Post模型save_files方法不返回true ..

我發佈這個答案,因爲也許有人會利用這個問題作爲自己的問題的答案。此處,我將我的return true

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 

    return true # <--------- Need to be added! 

end 
16

我很抱歉,但我只能就能夠推薦一下我們使用:


回形針

我很欣賞你正在使用的教程,但我強烈推薦使用Paperclip gem

該處理所有繁重的你:

#GemFile 
gem "paperclip", "~> 4.1.1" 

型號

#app/models/post.rb 
Class Post < ActiveRecord::Base 
    has_attached_file :image 
end 

#migration 
add_attachment :posts, :image 

控制器

#app/controllers/posts_controller.rb 
def new 
    @post = Post.new 
end 

def create 
    @post = Post.new(post_params) 
end 

private 

def post_params 
    params.require(:post).permit(:image, :other, :params) 
end 

查看

#app/views/posts/new.html.erb 
<%= form_for @post do |f| %> 
    <%= f.file_field :image %> 
<% end %> 
+8

回形針是偉大的,我也建議它作爲一個正確的應用程序時,要走的路,但是當學習有很多要說的知道如何做自己的事情,而不是把它交給一個神奇的寶石要做到這一點。我其實認爲「魔術」是Rails對學習如何使用它的最大問題。 –

+1

是的,我正在使用教程..感謝您的建議!當然我upvote你的答案,但我寧願按照我的教程,並找到我現有的代碼中的問題。如果沒有更多的答案能夠幫助我使用我的代碼,我會接受你的答案並嘗試一下「回形針」。謝謝! –

+2

當然 - 我只是想突出你的選擇:) –

0

如果您要存儲到數據庫中,您可能需要將圖像保存爲二進制格式。

require 'base64' 
base64_encoded = Base64.encode64(img_from_file_form) 

不知怎麼找回它來顯示圖像。

File.open('image_file.gif', 'wb') do|f| 
    f.write(Base64.decode64(base_64_encoded_data)) 
end 
+0

因爲我使用的教程,它應該以這種方式正常工作......我認爲有一個問題,我的Rails 4.1和教程Rails 2 .. –

0

嘗試在你的表單添加標籤的enctype =多/ FORMDATA如果您使用的形式來發表您的數據

+1

添加一些解釋與答案如何這個答案幫助OP在解決當前問題 –