Multi-step form in Rails 3 with Paperclip attachments

房东的猫 提交于 2019-11-28 07:36:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/5876714/multi-step-form-in-rails-3-with-paperclip-attachments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!