2

I have a form for uploading an excel file, and a method that parses said file using Roo. What I want to happen is this: user uploads excel file, and is redirected to a page that lists the first ten lines of the file.

I can get the first ten lines, but I can't figure out how to get them past the first method. I tried including them in the params, but I get a "Request-URI Too Large" error, unsurprisingly. This is what I have so far:

def upload_file
  # View with form. User adds file, which is uploaded under "file" params.
end

def process_file
  file_path = params[:file].path
  @lines = Roo::Excelx.new(file_path).first(10).to_a # Returns array of first ten lines of file
  ? # Somehow save value of @lines for "view_lines" method
  redirect_to view_lines_path
end

def view_lines
  @lines = ? # I want to use the "@lines" array from the previous method here.
  ...

So to be clear, I want to take the @lines value (which is an array of ten arrays) from the process_file method, and pass it through to the view_lines method. But it's too big to pass with params. Do I have to resort to using ajax?

Jeff Caros
  • 919
  • 5
  • 12
  • 32

3 Answers3

4

It looks like you just need to use a parameter when you define the view_lines method:

def process_file
  file_path = params[:file].path
  lines = Roo::Excelx.new(file_path).first(10).to_a # Returns array of first ten lines of file
  view_lines(lines)
  ...   
end

def view_lines(lines)
  # Use `lines` here
  lines.each do |line|
    # Use `line` here for each individual line
    ...
  end
end

You might also look over Variable Scope and Visibility. Unlike other languages such as Perl and shell scripting, the @ sigil doesn't indicate an array, but rather an instance variable.

It also sounds like you have another problem with trying to form an overly-long URL. If you are passing lots of data via HTTP, consider using a POST request.

Graham
  • 7,431
  • 18
  • 59
  • 84
Kathryn
  • 1,557
  • 8
  • 12
4

You can do it in multiple ways, two listed below.

First way, Store your session in table. Then you can set @lines in session and use that session in next request.

sessions['lines'] = @lines

Second way, If URL not important for your application. Then you can directly represent last 10 lines using render instaed of redirect.

def process_file
  file_path = params[:file].path
  @lines = Roo::Excelx.new(file_path).first(10).to_a # Returns array of first ten lines of file
  ? # Somehow save value of @lines for "view_lines" method
  render '/path/to/view'
end
Dipak Gupta
  • 7,321
  • 1
  • 20
  • 32
1

Looks like you don't want to pass to a method but wants to pass to a action. Just make it a post action instead of get. Post will handle that much data.

Thanks