如何在文件系统Ruby on Rails中存储图像

时间:2011-03-23 09:21:10

标签: ruby-on-rails ajax database-design paperclip

两种模式。

资产 - 用于使用回形针上传文件。

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

Boards - 博客

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 %>

这必须在另一种形式之外,否则它将无效,所以我在&lt;%end%&gt;之后的底部显示它另一种形式的标签。

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

使用资产对象上传文件。 文件存储在数据库的资产表中 照片网址存储在表单上的隐藏字段中。 创建了Board,并将照片URL存储在Boards表中。

因此,我将照片数据存储在两张似乎不正确的表中。

我是新手,绿色和以往一样。

将用于上传图片而非图片网址的Asset实例的asset.id存储到Board表中会更好吗?

更清楚:

我的Boards表中是否应该有一个字段

t.column asset_id

然后以某种方式访问​​资产照片数据?

非常感谢immensley提供的专业知识。

2 个答案:

答案 0 :(得分:0)

实际上,为什么不将附件连接到您的电路板型号而不是为它们分别设置两个型号?你在使用其他型号的资产吗?如果没有,最好将has_attachment放在board.rb模型中

答案 1 :(得分: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时,它将触发所有回形针方法,就像文件被上传一样,因为它们在保存项目时被触发。因此,当我有一个电路板时,它们将从临时位置复制到我希望它们驻留的位置。然后我必须删除资源表中的临时目录和组件,因为它完成了它的工作。

更新:现在一切都很完美。我已经纠正了上面的代码。

重要提示!!!!!

保存新模型后,您必须执行以下操作,否则路径值将是错误的。

model.photo.reprocess!

(用你的模型替换模型。在我的情况下,它是@ board.photo.reprocess!)

这是我的最终答案