如何上传在轨道上的红宝石文件? [英] How to upload a file in ruby on rails?

查看:169
本文介绍了如何上传在轨道上的红宝石文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在轨道上的红宝石很新。我遇到了一个问题。我想做一个文件上传功能,通过它我可以上传任何类型的文件(文本,图像等)。
我的控制器文件是(upload_controller.rb):

  class UploadController< ApplicationController 
def index
render:file => 'app \views\upload\uploadfile.html.erb'
end
def uploadFile
post = DataFile.save(params [:upload])
render:text => 文件已成功上传
结束
结束

我的模型文件是(data_file.rb):

  class DataFile< ActiveRecord :: Base 
attr_accessor:upload
def self.save(upload)
name = upload ['datafile']。original_filename
directory ='public / data'
#创建文件路径
path = File.join(directory,name)
#写入文件
File.open(path,wp){| f |阅读)}
end
end



 < h1>文件上传< / h1>我的查看文件是(uploadfile.html.erb) 
<%= form_tag({:action =>'uploadFile'},:multipart => true)do%>
< p>< label for =upload_file>选择文件< / label>
<%= file_field'upload','datafile'%>< / p>
<%= submit_tag上传%>
<%end%>

现在当我尝试上传图片时,在模型中出现错误invalid access mode wp文件。当我在模型文件中将File.open(路径,wp)更改为File.open(path,w)时,发出错误'\x89'从ASCII-8BIT到UTF-8。对于.txt文件,它工作正常。
我正在使用ruby 1.9.3和rails 3.2.6

解决方案

谢谢你的例子,我学习rails



它在rails 3.1中工作

我的代码:

 路线
资源:图片
集合{post:upload_image}
结束

控制器

  class ImagesController< ApplicationController 
def index
@car = Car.find(params [:car_id])
@images = @ car.images.order(order_id)
end

def upload_image
DataFile.save_file(params [:upload])
redirect_to images_path(:car_id => params [:car_id])
end

查看index.html.erb

 < h1>档案上载< / h1> 
<%= form_tag({:action =>'upload_image',:car_id => @ car.id},:multipart => true)do%>
< p>< label for =upload_file>选择文件< / label>
<%= file_field'upload','datafile'%>< / p>
<%= submit_tag上传%>
<%end%>

<%@ images.each do | image | %GT;
<%= image.id%>< br />
<%= image.name%>
<%end%>

模型

  class DataFile< ActiveRecord :: Base 
attr_accessor:upload
$ b $ def self.save_file(upload)
$ b file_name = upload ['datafile']。original_filename if(upload ['datafile ']!='')
file = upload ['datafile']。read
$ b file_type = file_name.split('。')。last
new_name_file = Time.now .to_i
name_folder = new_name_file
new_file_name_with_type =#{new_name_file}。 + file_type
$ b image_root =#{RAILS_CAR_IMAGES}


Dir.mkdir(image_root +#{name_folder});
File.open(image_root +#{name_folder} /+ new_file_name_with_type,wb)do | f |
f.write(file)
end

end
end


I’m very new in ruby on rails. I’m stuck with a problem. I want to make a file upload functionality through which I can upload any kind of file (text,image etc.). My controller file is (upload_controller.rb):

class UploadController < ApplicationController
def index
    render :file => 'app\views\upload\uploadfile.html.erb'
end
def uploadFile
    post = DataFile.save(params[:upload])
    render :text => "File has been uploaded successfully"
end
end

My Model file is (data_file.rb):

class DataFile < ActiveRecord::Base
    attr_accessor :upload
  def self.save(upload)
    name = upload['datafile'].original_filename
    directory = 'public/data'
    # create the file path
    path = File.join(directory,name)
    # write the file
    File.open(path, "wp") { |f| f.write(upload['datafile'].read)}
  end
end

My View file is (uploadfile.html.erb):

<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>

Now when I try to upload image then I'm getting error "invalid access mode wp" in model file. When I change File.open(path, "wp") to File.open(path, "w") in model file this give error "'\x89' from ASCII-8BIT to UTF-8". For .txt file, It works fine. I'm using ruby 1.9.3 and rails 3.2.6

解决方案

Thank you for example, I study rails too!

It works in rails 3.1

My code:

Routes
resources :images do
      collection { post :upload_image }
    end

Controller

class ImagesController < ApplicationController
  def index
    @car = Car.find(params[:car_id])
    @images = @car.images.order("order_id")
  end

  def upload_image   
    DataFile.save_file(params[:upload])
    redirect_to images_path(:car_id => params[:car_id])
  end

View index.html.erb

<h1>File Upload</h1>
  <%= form_tag({:action => 'upload_image', :car_id => @car.id}, :multipart => true) do %>
    <p><label for="upload_file">Select File</label>
    <%= file_field 'upload', 'datafile' %></p>
    <%= submit_tag "Upload" %>
  <% end %>

  <% @images.each do |image| %>
     <%= image.id %><br/>
     <%= image.name %>
  <% end %>

Model

class DataFile < ActiveRecord::Base
    attr_accessor :upload

  def self.save_file(upload)   

    file_name = upload['datafile'].original_filename  if  (upload['datafile'] !='')    
    file = upload['datafile'].read    

    file_type = file_name.split('.').last
    new_name_file = Time.now.to_i
    name_folder = new_name_file
    new_file_name_with_type = "#{new_name_file}." + file_type

    image_root = "#{RAILS_CAR_IMAGES}"


    Dir.mkdir(image_root + "#{name_folder}");
      File.open(image_root + "#{name_folder}/" + new_file_name_with_type, "wb")  do |f|  
        f.write(file) 
      end

  end
end

这篇关于如何上传在轨道上的红宝石文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆