One way to fix the problem is to define file_upload as a class method and call this method in the controller.
index.html.erb
= 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"
Model
def self.file_upload uploaded_file begin file = Tempfile.new(uploaded_file, '/some/other/path') returning File.open(file.path, "w") do |f| f.write file.read f.close end ensure file.close file.unlink
controller
def index if request.post? @contents = Model.file_upload(params[:uploaded_file]) end end
You will need to apply sanity checks and more. Now that @contents defined in the controller, you can use it in the view.
source share