簡體   English   中英

如何在文件系統Ruby on Rails中存儲圖像

[英]how to store image in file system 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 %>

這必須在另一種形式之外,否則將無法正常工作,因此我在另一種形式的<%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

文件與資產對象一起上傳。 文件存儲在資產表中,數據庫照片url存儲在表單上的隱藏字段中。 創建木板,並將照片URL存儲在“木板”表中。

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

我是一個新手,像往常一樣綠色。

將用於上傳圖像的Asset實例的asset.id而不是圖像url存儲到Board表中,是否更好?

要更清楚:

我應該在“ Boards”表中有一個字段嗎

t.column asset_id

然后以某種方式訪問​​資產照片數據?

預先感謝immensley的專業知識。

實際上,為什么不將附件僅連接到您的電路板模型,而不是為它們配備兩個單獨的模型? 您是否將資產用於其他模型? 如果不是,最好將has_attachment放在board.rb模型中

我在這里找到答案

當我將信息從@asset對象傳輸到board對象時。

在Board.rb中,按照建議的方式進行操作並添加回形針項目

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

因此,我認為我不需要params的木板照片。 我從@ asset.photo.url的URL創建它。

   @board.photo = @asset.photo

當我執行@ board.save時,它將觸發所有回形針方法,就好像文件正在上載一樣,因為在保存項目時會觸發它們。 因此,將它們從臨時位置復制到當我有板子時我希望它們駐留的位置。 然后,在完成工作時,我必須刪除temp目錄和資產表中的資產。

更新:現在一切正常。 我已更正上面的代碼。

重要的提示!!!!!

保存新模型后,必須執行以下操作,否則url和path值將錯誤。

model.photo.reprocess!

(用您的模型替換模型。在我的情況下是@ board.photo.reprocess!)

那是我的最終答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM