Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-step form in Rails 3 with Paperclip attachments

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.

like image 843
Jeremy White Avatar asked May 03 '11 23:05

Jeremy White


1 Answers

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.

like image 107
KARASZI István Avatar answered Nov 14 '22 05:11

KARASZI István