Rails 3 中带有回形针附件的多步骤表单
我正在按照 Ryan Bates 此处描述的样式创建一个多部分表单:
http://railscasts .com/episodes/217-multistep-forms
http://asciicasts.com/episodes/217-multistep-forms(基于文本版本)
总而言之,我有一个视图(每个表单步骤都有一堆部分),并且当用户单击下一步按钮并显示表单的不同部分时,表单变量存储在会话中。
我的表单步骤之一允许用户通过回形针 gem 上传多个图像。问题是 Rails 正在尝试将图像数据上传到会话,该会话返回 TypeError“无法转储文件”。
解决这个问题的好方法是什么?
更新: 我已经尝试了一堆 gems(wizardly、acts_as_wizard 和其他一些较小的),但它们似乎都不适用于 Rails 3。
我也尝试过将数据存储在数组中,直到表单完成,但是那导致我的控制器变得庞大而混乱。
I'm creating a multi-part form in the style that Ryan Bates describes here:
http://railscasts.com/episodes/217-multistep-forms
http://asciicasts.com/episodes/217-multistep-forms (text-based version)
To summarize, I have one view (with a bunch of partials for each form step), and the form variables are stored in a session when the user clicks a next button and a different part of the form is displayed.
One of my form steps allows the user to upload several images via the Paperclip gem. The problem with that is that Rails is trying to upload the image data to the session, which is returning TypeError "can't dump File".
What is a good way to go about this?
UPDATE:
I've tried a bunch of gems (wizardly, acts_as_wizard, and some other smaller ones) but none of them seem to work with Rails 3.
I've also tried just storing the data in an array until the form is complete, but that was causing my controller to get huge and messy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非您想将
文件
保存到会话中,否则将模型保存到会话中是可行的。向导插件使用会话来存储步骤之间的模型。在我的例子中,它们不会在有效模型上产生错误,仅在无效模型上产生错误。因此,清除附加文件听起来是个好主意,但就我而言,使用
Attachment#clear
还不够,因为它仍然想保存一些文件
。我发现问题出在
@
属性仍然包含数据。Attachment
中的queued_for_write因此,以下两行解决了我的问题:
这是一个回形针错误,并在此提交。
Saving models into the session is working unless you want to save a
File
into the session. The wizard plugins are using the session to store models between the steps. They do not produce errors on valid models in my case only on invalids.So clearing out the attached file sounded a good idea, but in my case clearing out the paperclip attachment with
Attachment#clear
was not enough because it still wanted to save someFile
.I've found out that the problem was with the
@queued_for_write
attribute inAttachment
which still contained the data.So the following two lines solved my problem:
This was a paperclip bug and was corrected in this commit.