Rails 3 中带有回形针附件的多步骤表单

发布于 2024-11-04 13:37:15 字数 634 浏览 0 评论 0原文

我正在按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我鬧 2024-11-11 13:37:15

除非您想将文件保存到会话中,否则将模型保存到会话中是可行的。向导插件使用会话来存储步骤之间的模型。在我的例子中,它们不会在有效模型上产生错误,仅在无效模型上产生错误。

因此,清除附加文件听起来是个好主意,但就我而言,使用 Attachment#clear 还不够,因为它仍然想保存一些文件

我发现问题出在 @ Attachment 中的queued_for_write 属性仍然包含数据。

因此,以下两行解决了我的问题:

unless @model.valid?
  @model.image.clear
  @model.image.queued_for_write.clear
end

这是一个回形针错误,并在此提交。

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

I've found out that the problem was with the @queued_for_write attribute in Attachment which still contained the data.

So the following two lines solved my problem:

unless @model.valid?
  @model.image.clear
  @model.image.queued_for_write.clear
end

This was a paperclip bug and was corrected in this commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文