简体   繁体   English

在同一控制器中的方法之间传递实例变量不在Rails中工作

[英]Passing instance variable between methods in same controller not working in Rails

I have a multi stage form I'm trying to process. 我有一个我正在尝试处理的多阶段表单。 One of the later stages needs to use data set by a previous stage. 后一阶段之一需要使用前一阶段的数据集。 I had thought that by setting the data I need in an instance variable, eg 我曾经想过通过在实例变量中设置我需要的数据,例如

@gateway = importer.get_gateway

I could then access it in a different controller method of the same controller class, eg 然后我可以在同一控制器类的不同控制器方法中访问它,例如

Rails.logger.info "populate_devices : gateway is #{@gateway}"

However in the logs @gateway is nil in this next step of the form submission. 但是在日志中@gateway在表单提交的下一步中是nil Should it be working, or is this not the correct way to pass data from one controller method to another in the same controller class? 它应该工作,还是这不是在同一控制器类中将数据从一个控制器方法传递到另一个控制器方法的正确方法? @gateway is definitely being set to not nil in the previous step as I display some of it's fields on the UI and have logged it. @gateway肯定在上一步中设置为nil ,因为我在UI上显示了一些字段并记录了它。

I think you should store it in session, because Rails is a stateless technology. 我认为你应该将它存储在会话中,因为Rails是一种无状态技术。 There are limitations in size, but only if you choose to use a cookie session store (up to 4k). 大小有限制,但前提是您选择使用cookie会话存储(最多4k)。

If you decide to use the ActiveRecord session store or memcached store, there are no limits. 如果您决定使用ActiveRecord会话存储或memcached存储,则没有限制。 They are easy to implement and fast. 它们易于实施且快速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM