如何将以下rails 2.3.x代码转换为3.2.13?

时间:2013-07-25 10:37:34

标签: ruby-on-rails ruby-on-rails-3

这是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

1 个答案:

答案 0 :(得分: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

我认为它应该有用。