Ruby on rails 如何通过表单获取临时文件的内容
index.html.erbRuby on rails 如何通过表单获取临时文件的内容,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,ruby-on-rails-3.2,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Ruby On Rails 3.2,index.html.erb = form_for :file_upload, :html => {:multipart => true} do |f| = f.label :uploaded_file, 'Upload your file.' = f.file_field :uploaded_file = f.submit "Load new dictionary" = form_for :index, :html => {:multip
= form_for :file_upload, :html => {:multipart => true} do |f|
= f.label :uploaded_file, 'Upload your file.'
= f.file_field :uploaded_file
= f.submit "Load new dictionary"
= form_for :index, :html => {:multipart => true} do |f|
= f.label :uploaded_file, 'Upload your file.'
= f.file_field :uploaded_file
= f.submit "Load new dictionary"
模型
索引
但是,在我上传文件后,我的页面上没有打印任何内容。请使用
文件。阅读阅读上传文件的内容:
def file_upload
@contents = params[:uploaded_file].read
# save content somewhere
end
解决此问题的一种方法是将文件\u upload
定义为类方法,并在控制器中调用该方法
index.html.erb
= form_for :file_upload, :html => {:multipart => true} do |f|
= f.label :uploaded_file, 'Upload your file.'
= f.file_field :uploaded_file
= f.submit "Load new dictionary"
= form_for :index, :html => {:multipart => true} do |f|
= f.label :uploaded_file, 'Upload your file.'
= f.file_field :uploaded_file
= f.submit "Load new dictionary"
模型
控制器
def index
if request.post?
@contents = Model.file_upload(params[:uploaded_file])
end
end
你需要进行精神检查之类的。既然控制器中定义了@contents
,您就可以在视图中使用它了。如何将内容发送回索引您是如何将参数[:upload_file]
发送到您的模型中的?这看起来像控制器代码
def index
if request.post?
@contents = Model.file_upload(params[:uploaded_file])
end
end