ruby-on-rails - 如何在Rails上上传ruby文件?

标签 ruby-on-rails ruby-on-rails-3 file-upload

我是 ruby 的新手。我遇到了问题。我想制作一个文件上传功能,通过它我可以上传任何类型的文件(文本,图像等)。
我的 Controller 文件是(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

我的模型文件是(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

我的查看文件是(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 %>

现在,当我尝试上传图像时,我在模型文件中收到“无效的访问模式wp”错误。当我在模型文件中将File.open(path,“wp”)更改为File.open(path,“w”)时,这会导致错误“\ x89”从ASCII-8BIT变为UTF-8。对于.txt文件,它可以正常工作。
我正在使用ruby 1.9.3和rails 3.2.6

最佳答案

例如,谢谢您,我也学习护栏!

它可以在Rails 3.1中工作

我的代码:

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

控制者
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>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 %>

模型
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

关于ruby-on-rails - 如何在Rails上上传ruby文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11342338/

相关文章:

ruby-on-rails - 'attr_accessible' 效果

php - 通知: Unknown: file created in the system's temporary directory in Unknown on line 0

javascript - 仅显示队列中最后一项的进度。 ng文件上传

javascript - 如果 ajax 失败,Dropzone.js 重试

ruby-on-rails - Ruby 中的 ->() { } 是什么?

mysql - Ruby, Rails : mysql2 gem, 有人使用这个 gem 吗?稳定吗?

css - 使用 bootstrap-sass gem 覆盖 Rails 中的 Bootstrap 变量

ruby-on-rails - Rails 回形针图像 URL 上传不起作用

ruby-on-rails - Rails,将多个文件上传到相册

ruby - 无法使用 facebook 进行身份验证 : undefined method 'web_server' for OAuth2?