如何在文件系统 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
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 %>
这必须在其他形式之外,否则它将无法工作,因此我将其放在 <% 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
文件通过 assets 对象上传。 文件存储在数据库的assets表中 照片 url 存储在表单的隐藏字段中。 Board 已创建,照片 url 存储在 Boards 表中。
因此,我将照片数据存储在两个表中,这似乎不正确。
我是一个新手,一如既往的新手。
将用于上传图像的 Asset 实例的 asset.id 而不是图像 url 存储到 Board 表中是否更好?
更清楚地说:
我应该在 Boards 表中有一个字段
t.column asset_id
,然后以某种方式访问资产照片数据吗?
预先感谢 immensley 的专业知识。
Two models.
Assets - used to upload files with 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 - a blog
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
There is an ajax iframe work around at the bottom of this form. The user can upload a picture and view it on the form before submitting it. Therefore there is no board that can be saved to upload the picture with. So I had to create the asset object.
<% 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 %>
This has to be outside of the other form or it won't work so I have it here at the bottom after the <% end %> tag of the other form.
iframe ajax style file upload of a picture.
<% 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;
},
});
});
});
In 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
In board_controller
def create
...
@board.new(params[:board])
...
end
File gets uploaded with assets object.
File is stored in assets table in database
photo url is stored in the hidden field on the form.
Board is created and photo url is stored in the Boards table.
Thus I have stored photo data in two tables which does not seem correct.
I am a newbie, green as there ever was.
Is it better to store the asset.id of the Asset instance used to upload the images instead of the image url into the Board table?
To be clearer:
Should I have a field in my Boards table
t.column asset_id
and then access the assets photo data someway?
Thanks immensley in advance for your expertise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,为什么不直接将附件连接到您的电路板模型,而不是为它们提供两个单独的模型呢?您是否正在将这些资源用于其他模型?如果没有,最好将 has_attachment 放入 board.rb 模型中
Actually, why not just connect the attachments to your board model instead of having two separate models for them? are you using the assets for your other models? If not, it is better to just put the has_attachment in your board.rb model
我找到了答案在这里< /a>
当我将信息从@asset对象传输到board对象时。
在 Board.rb 中,我按照 Corroded 的建议进行操作并添加回形针项目,
因此我认为我不需要参数中的板照片。我从 @asset.photo.url 的 url 创建它。
当我执行 @board.save 时,它将触发所有回形针方法,就像上传文件一样,因为它们在保存项目时被触发。因此,当我有董事会时,它们将从临时位置复制到我希望它们驻留的位置。然后,我必须在完成其工作后删除资产表中的临时目录和资产。
更新:现在一切正常。我已经更正了上面的代码。
重要提示!!!!
保存新模型后必须执行以下操作,否则 url 和路径值将错误。
模型.照片.重新处理!
(用你的模型替换模型。在我的例子中它是@board.photo.reprocess!)
这是我的最终答案
I found the answer here
When I transfer the information from the @asset object to the board object.
In Board.rb I do as corroded suggested and add the paperclip item
So I don't think I need the boards photo from params. I create it from the url of the @asset.photo.url.
When I do @board.save it will trigger all of the paperclip methods as if the file was being uploaded because they get triggered when the items are saved. So they will be copied from the temp location to where I want them to reside when I have a board. I then have to delete out the temp directories and the assest in the assets table as its done its job.
Update: It is all working perfectly now. I have corrected the code above.
important note!!!!!
you must do the following after you save the new model or the url and path values will be wrong.
model.photo.reprocess!
(replace model with your model.In my case it was @board.photo.reprocess!)
That is my final answer