0

这是我在 Rubymine 中用于 rails 2.3.x 版的 ruby​​ 代码。我想将以下代码转换为 rails 3.2.13,因为 rails 3.2.13 不支持此代码。

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

我的控制器代码是:-

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

我的查看代码是

<h1>File Upload</h1>
<%= form_for (:uploadFile, :multipart => true) do |f|%>
    <%= f.label :upload, 'Upload File' %>:
    <%= f.file_field :upload %></p>
    <%= f.submit "Upload" %>
<% end %>

我的 route.rb 文件中的代码是

Uploadfile::Application.routes.draw do
get "upload/uploadfile"

模型中的代码是

class DataFile < ActiveRecord::Base
  # attr_accessible :title, :body
  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, "wb") { |f| f.write(upload['datafile'].read) }
  end
end

如问题中所述,我希望代码运行到 rails 3.2.13

4

1 回答 1

0

试试这个:

<h1>File Upload</h1> 
  <%= form_for :file_upload, :html => {:multipart => true) do |f|%> 
  <%= f.label :upload, 'Upload File' %>:
  <%= f.file_field :upload %></p> 
  <%= f.submit "Upload" %> 
  <% end %>

在您的控制器中:

class UploadController < ApplicationController

  def file_upload
    post = DataFile.save(params[:file_upload])
    render :text => "File has been uploaded successfully"
  end
end

我的 route.rb 文件中的代码:

Uploadfile::Application.routes.draw do

resources :upload do
  get "file_upload"
end

我认为它应该工作。

于 2013-07-25T11:06:59.303 回答