在 ruby on Rails 中的控制器操作之间共享数据
我目前正在开发一个需要解析一些(大文件)csv 数据的应用程序。我必须执行几个步骤来操作数据并将其映射到 ActiveRecord 对象。这必须分几个步骤完成,并且我必须在视图中显示结果。
所以我从 import#index 开始。在这里,我让用户上传 csv 文件,没什么花哨的
然后我通过 import#create 来创建导入对象。
然后我在第 2 步导入#show。我在这里显示结果。因此,在 import#show 中,我解析了所有 csv 数据并删除了不需要的行。 (这是一个包含结构的大数组。)
然后我想转到步骤 3 进一步操作相同的数据。但是,我到底如何才能在没有会话的情况下从步骤 3 中的步骤 2 获取大数组(由于结构,我无法将其插入到会话中,它给了我以下错误:TypeError(无法转储匿名类 #<) ;Class:0x105477128>):
当我将它放入 .inspect 的会话中时,它会引发此错误: ActionDispatch::Cookies::CookieOverflow
但是这都是一个大字符串,我想维护 ruby 功能(例如,它是一个包含结构对象的大数组...)
所以将其全部包装起来。向上
步骤 1 上传文件 步骤2 解析数据&操纵它。 步骤3 使用步骤2中的数据(没有会话..) 步骤4 步骤 3 中的用户数据
我到底怎样才能让它发挥作用...我似乎无法弄清楚:<
I'm currently developing an application that needs to parse some (a big file of) csv data. I have to perform several steps to manipulate and map the data to ActiveRecord objects. This has to be done in several steps, and i have to display the results in a view.
So i start at imports#index. Here i let the users upload the csv file, nothing fancy
Then i go trough imports#create to create the import object.
Then i'm at step2 imports#show. Here i display the results. So in import#show i parsed all the csv data and delete the rows that i dont need. (It's one big array with structs in it.)
Then i want to go to step 3 to manipulate the same data further. But how in the world can i get the big array from step2 in step3 without a session (because of the Structs i cant insert it in a session it gives me the following error: TypeError (can't dump anonymous class #<Class:0x105477128>):
And when i put it in a session with .inspect it raises this error: ActionDispatch::Cookies::CookieOverflow
. But then it's all one big string and i want to maintain the ruby functionality (e.g. it's 1 big array with struct objects in it...)
So to wrap it all up
Step 1
upload file
Step2
parse data & manipulate it.
Step3
Use the data from step2 (without a session..)
Step4
User data from step3
How in the world can i get this to work... I can't seem to figure it out :<
您应该记住不同的原则:
控制器不是用来处理数据的,模型或类是用来处理的
解析一个大文件不应该'不是在控制器内部制作的,而是放入某个队列中(延迟作业可以正确处理此问题,请参阅它如何工作此处)。想象一下您的用户无休止地等待某事发生...
总结一下:
第 1 步:上传文件 =>好的,我猜你保存了,太棒了
Step2 解析数据&操纵它=>应该由步骤 1 触发并在延迟作业队列中启动
步骤 3 使用步骤 2 中的数据(没有会话..) =>按照我建议的方式处理的所有数据都可以存储在缓存、数据库或任何您认为有用的地方
Step4 步骤 3 中的用户数据 =>如果您想显示处理结果,我建议您实现一个 AJAX 请求来检查作业是否完成(为此,我强烈建议您将信息存储在缓存中,以免过于频繁地查询数据库)。
You should keep in mind different principles:
Controllers are not meant to deal with data, Models or Classes do
parsing a big file shouldn't be made inside the controller but put inside some queue (Delayed Job handles this properly, see how it works here). Just imagine your user waiting endlessly for something to happen...
To sum up:
Step 1: upload file => ok I guess you save it, great
Step2 parse data & manipulate it => should be triggered by step 1 and launched in a Delayed Job queue
Step3 Use the data from step2 (without a session..) => all data processed the way I suggest could be stored in cache, database or wherever you find it useful
Step4 User data from step3 => you want to display the result of your processing, I suggest you implement an AJAX request checking if the job was done or not (to do that, I highly recommend you store the info in cache in order not to query your database too often).