これは、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
質問で述べたように、コードをレール 3.2.13 で実行したい