带有回形针附件的Rails 3中的多步表单 [英] Multi-step form in Rails 3 with Paperclip attachments

查看:177
本文介绍了带有回形针附件的Rails 3中的多步表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



http://railscasts.com/episodes/217-multistep-forms

http://asciicasts.com/episodes/217-multistep-forms (基于文本的版本)



总而言之,我有一个视图(每个表单步骤都有一些分支),当用户单击下一个按钮并且表单的不同部分是显示。



我的一个表单步骤允许用户通过Paperclip gem上传几张图片。问题在于Rails正尝试将图像数据上传到会话中,该会话正在返回TypeError无法转储文件。



什么是好方法去这个吗?

更新:
我试过了一堆宝石(向导,acts_as_wizard,和其他一些小的),但他们都没有使用Rails 3。



我也尝试将数据存储在数组中,直到表单完成,但是这导致我的控制器变得巨大和凌乱。

解决方案

将模型保存到会话中除非要保存文件进入会话。向导插件正在使用会话在步骤之间存储模型。他们不会在我的情况下只在invalids上产生有效模型的错误。



所以清除附加文件听起来不错,但在我的情况下清除回形针附件使用 附件#清除 还不够,因为它仍然想保存一些文件



我找到了问题出在 @queued_for_write 属性在附件中仍然包含数据。



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

 除非@ model.valid? 
@ model.image.clear
@ model.image.queued_for_write.clear
end

这是一个回形针错误,并在此提交中得到纠正。


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.

解决方案

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.

这篇关于带有回形针附件的Rails 3中的多步表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆