8

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"

モデル

def file_upload
    file = Tempfile.new(params[:uploaded_file])
    begin
        @contents = file
    ensure
        file.close
        file.unlink   # deletes the temp file
    end
end

索引

def index
    @contents
end

しかし、ファイルをアップロードした後、ページに何も印刷されません= @contents

4

2 に答える 2

7

file.readアップロードされたファイルの内容を読み取るために使用します。

def file_upload
  @contents = params[:uploaded_file].read
  # save content somewhere
end
于 2013-05-20T12:24:50.883 に答える