2011-03-23 121 views
0

兩種模式。如何在文件系統中存儲圖像Ruby on Rails

資產 - 用於使用回形針上傳文件。

class AddAttachmentsPhotoToAsset < ActiveRecord::Migration 
    def self.up 
    add_column :assets, :photo_file_name, :string 
    add_column :assets, :photo_content_type, :string 
    add_column :assets, :photo_file_size, :integer 
    add_column :assets, :photo_updated_at, :datetime 
    end 

板 - 一個博客

class CreateBoards < ActiveRecord::Migration 
    def self.up 
    create_table :boards, :options => "AUTO_INCREMENT = 1000" do |t| 
     t.column :deliver_on, :datetime 
     t.column :time_zone,  :string 
     t.column :header_text, :text 
     t.column :name,   :string 
     t.column :age,   :int, :default => "0" 
     t.column :template_id, :int, :default => "1", :null => false 
     t.column :photo, :string 
     t.timestamps 
    end 
    end 

new.html.erb

有一個Ajax的iframe工作圍繞在這種形式的底部。用戶可以上傳圖片並在提交之前在表格上查看圖片。因此沒有可以保存的板子來上傳圖片。所以我必須創建資產對象。

<% form_tag(action, :id => "form") do %> 
<%=image_tag board_image, :width => 140, :height => 166, :id => 'user_image' %><%= hidden_field :board, :photo, :id => 'image_name'%> 

<%= error_messages_for :user,:board, :header_message => "" %> 
<%= label_tag :name, "Friend's Name:", :class => "large"%> 
<%= text_field :board, "name", :class => "large", :style => Board::NAME_WIDTH %>  
. 
. 
. 
<%=submit_tag "#{button_text}", :id => "board_submit"%> 
<% end %> 

這有可能是其他形式之外,否則將無法工作,所以我把它在這裏的其他形式的<%結束%>標記之後的底部。

iframe ajax樣式文件上傳的一張圖片。

<% remote_form_for(:asset, :url => { :controller => "asset", :action => "upload", :id => @tmp_id }, :html => { :method => :post, :id => 'uploadForm', :multipart => true }) do |f| %> 
<%= f.file_field :photo, :class => 'file' %> 
<% end %> 

upload.js

//ajax file upload 
     $(function() { 
     $("#uploadForm input").change(function(){ 
     $("#uploadForm").ajaxSubmit({ 
      beforeSubmit: function(a,f,o) { 
      o.dataType = "json"; 
      }, 
      complete: function(XMLHttpRequest, textStatus) { 
      $('#user_image').attr('src', XMLHttpRequest.responseText +'?'+ new Date().getTime()); 
      $('#image_name').attr('value', XMLHttpRequest.responseText); 
      return; false; 
      }, 
     }); 
     }); 
     }); 

在assets_controller.rb

def upload 
     if params_posted?(:asset) #check that the form was submitted with a post action 
     @asset = Asset.new(params[:asset]) #create new paperclip object to upload item 
     @asset.save #upload the photo 
     render :text => @asset.photo.url #put the name on the form. 
     end 
    end 

在board_controller

def create 
... 
@board.new(params[:board]) 
... 


end 

文件獲取與資產Ø上傳bject。 文件存儲在資產表中的數據庫 照片URL存儲在窗體的隱藏字段。 電路板已創建,照片URL存儲在電路板表中。

因此,我已經將照片數據存儲在兩張表中,看起來不正確。

我是一個新手,一如既往的綠色。

將Asset實例的asset.id用於上傳圖片而不是圖片url到Board表中更好嗎?

更清晰:

我應該有一個外地在我的主板表

t.column asset_id 

,然後好歹訪問資產照片數據?

非常感謝immensley爲您提供的專業知識。

回答

0

我找到了答案here

當我從@asset對象傳遞信息板上的對象。

在Board.rb我一樣腐蝕的建議,並添加回形針項目

has_attached_file :photo #I can also add styles, where I want to save the photo etc 

所以我不認爲我需要從PARAMS板的照片。我從@ asset.photo.url的網址創建它。

@board.photo = @asset.photo 

當我做@ board.save它會觸發所有的回形針方法,如文件被上傳,因爲當被保存的物品,他們被觸發。因此,他們將從臨時位置複製到我希望他們在董事會所在的位置。然後,我必須刪除臨時目錄和資產表中的資產,因爲它完成了它的工作。

更新:現在都完美了。我已更正上面的代碼。

重要說明!!!!!

您必須在保存新模型後執行以下操作,否則URL和路徑值將會出錯。

model.photo.reprocess!

(替換爲您model.In我的情況下,它是@ board.photo.reprocess模型!)

這是我最後的答案

0

實際上,爲什麼不直接將附件連接到您的電路板模型,而不是讓它們有兩個獨立的模型?你是否在爲其他模型使用資產?如果沒有,最好把has_attachment放在你的板子裏。RB模型

+0

由於腐蝕。我想這樣做。問題是我在表單被保存之前上傳照片,以便用戶可以在表單上看到圖像。我無法使用板對象來保存它們,因爲這需要我保存板來上傳圖像。董事會不會保存,因爲它缺少所需的數據,如姓名或電子郵件地址。就我所見,還是我錯了? – chell 2011-03-23 10:58:54

+0

該關聯將爲:董事會belongs_to:資產資產has_one:董事會。這聽起來很奇怪。這就是爲什麼我不認爲它是正確的。我如何在理事會和資產之間建立一種有意義的聯繫。 – chell 2011-03-23 17:07:48

+0

董事會has_one:資產和資產belongs_to:董事會。那更符合邏輯吧?因爲它是具有資產 – corroded 2011-03-23 17:50:26