0
这是我的样式文件data_file.rb如何使用ruby mine将文件上传到目录中?
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作为IDE。任何人都可以请指导我?这将有很大的帮助。
....我试过你的解决方案。然后发生以下错误 - : 路由错误 没有路由匹配{:action =>“uploadFile”,:controller =>“upload”} 尝试运行耙路由以获取有关可用路由的更多信息。 –
任何人都可以帮忙吗? –
不要急于求成,我们不会为您提供帮助。另外,您可以使用空闲时间在Google上搜索自己的解决方案。无论如何,这个问题是另一个问题,但我仍然编辑我的答案尝试并帮助你 – Benj