Ruby on rails 使用Ruby Rails 4在aWebsite上上载文件
我使用的是Rails4,我有这个代码 我创建了一个控制器,并将其命名为upload_controller.rb 我把这个代码放进去了:Ruby on rails 使用Ruby Rails 4在aWebsite上上载文件,ruby-on-rails,ruby,Ruby On Rails,Ruby,我使用的是Rails4,我有这个代码 我创建了一个控制器,并将其命名为upload_controller.rb 我把这个代码放进去了: class UploadController < ApplicationController def index render :file => 'app\views\upload\uploadfile.rhtml.erb' end def uploadFile post = DataFile.save(params[:u
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.rhtml.erb'
end
def uploadFile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
我的模型名为data_file.rb。代码如下:
class DataFile < ActiveRecord::Base
attr_accessor :upload
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
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>
我的视图文件名为uploadFile.html.erb。我的代码如下:
class DataFile < ActiveRecord::Base
attr_accessor :upload
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
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' %></p>
<%= submit_tag "Upload" %>
<% end %>
我的主要目标是使用此代码,以便用户可以将文件上载到指定位置。文件类型必须是任何东西,不仅仅是图像,还有文档、excel表格等。一旦我写了这段代码,我进行了捆绑安装,然后我进行了rake db:migrate,我转到工具并运行开发。一旦我这样做了,我就转到firefox并输入localhost:3000,页面就无法显示。指示我去雅虎搜索或其他什么
我不知道我做错了什么。我是想加一块宝石还是换一行什么的?我已经被这个问题困扰了好几天了,我只想进入我网页的下一部分。请帮帮我。多谢各位 如果localhost:3000上没有发生任何事情,听起来您的服务器没有运行。你启动了吗?是的,你在rails应用程序目录的控制台中键入了“rails s”了吗?