Petya petrov
Petya petrov

Reputation: 2213

get content of local html file with rails

what i need is passing local html file to my form, get content of this file and later parse it. I have this in view

  = form_tag :parser, :html => {:multipart => true} do
    = file_field_tag :html_file
    = submit_tag

And this in controller

  def parser
    @file = params[:html_file]
  end

It seems like form dont get file, just get the string with name of file. How to fix it?

Upvotes: 0

Views: 909

Answers (1)

Teddy
Teddy

Reputation: 18572

I wrote this to handle uploads in a Rails 2.3.x app. I cannot remember why I had to split this into an if/elsif/else/end statement, but I must have had an error consolidating it to one if statement.

    if %w(File Tempfile ActionController::UploadedTempfile ).include?(params[:html_file].class.to_s)          
      data = params[:html_file].read
    elsif %w(StringIO ActionController::UploadedStringIO).include?(params[:html_file].class.to_s)
      data = params[:html_file].read
    else
      logger.error("File does not appear to be a valid class.")
    end

Upvotes: 2

Related Questions