开发者

Multi-step form in Rails 3 with Paperclip attachments

开发者 https://www.devze.com 2023-03-02 08:09 出处:网络
I\'m creating a multi-part form in the style that Ryan Bates describes here: http://rail开发者_Go百科scasts.com/episodes/217-multistep-forms

I'm creating a multi-part form in the style that Ryan Bates describes here:

http://rail开发者_Go百科scasts.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号