这是我的模态文件 data_file.rb
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
这是我的控制器文件
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_tag :action => 'uploadFile' do %>
<p><label for="upload_file">Select File</label> :
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<%= end %>
每当我尝试使用语法访问视图文件时http://127.0.0.1:3000/upload/index
.....我收到以下错误
显示 C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb 其中第 6 行提出:
C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:6: syntax error, unexpected keyword_end
');@output_buffer.append= ( end );@output_buffer.to_s
^
C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:7: syntax error, unexpected keyword_ensure, expecting ')'
C:/Users/pratik/RubymineProjects/upload/app/views/upload/uploadfile.html.erb:9: syntax error, unexpected keyword_end, expecting ')'
Extracted source (around line #6):
3: <p><label for="upload_file">Select File</label> :
4: <%= file_field 'upload', 'datafile' %></p>
5: <%= submit_tag "Upload" %>
6: <%= end %>
该项目是 tutorialpoints.com 上的示例项目。但是,当我尝试这样做时,它失败了。我正在使用 ruby mine 作为 IDE。有人可以指导我吗?这将有很大帮助。