gpt4 book ai didi

ruby-on-rails - 如何将长数组传递到另一个页面?

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:07 25 4
gpt4 key购买 nike

我有一个用于上传 excel 文件的表单,以及一个使用 Roo 解析所述文件的方法.我想要发生的是:用户上传 excel 文件,并被重定向到一个列出文件前十行的页面。

我可以得到前十行,但我不知道如何让它们通过第一种方法。我尝试将它们包含在参数中,但不出所料,我收到“Request-URI Too Large”错误。这是我目前所拥有的:

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.
...

明确地说,我想从 process_file 方法中获取 @lines 值(这是一个包含十个数组的数组),并将其传递给view_lines 方法。但是它太大了,无法通过参数传递。我必须求助于使用 ajax 吗?

最佳答案

您可以通过多种方式完成,下面列出了两种。

第一种方式,存储您的 session in table .然后您可以在 session 中设置 @lines 并在下一个请求中使用该 session 。

sessions['lines'] = @lines

第二种方式,如果 URL 对您的应用程序不重要。然后,您可以使用 render 代替 redirect 直接表示最后 10 行。

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

关于ruby-on-rails - 如何将长数组传递到另一个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44752577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com