However, the rule that models don't belong in the session is trumped by the rules that POSTs should yield GETs and GETs should not mutate state. Every page should be reloadable, without an annoying pop-up asking if you want to resubmit a form. POSTs are allowed to be dangerous. POSTs are allow to charge your credit card, delete records or log you out. So, when creating or editing a model, if the model fails to validate, I refuse to display an error page as a direct result of a POST. Instead, the user's browser is redirected back to the new or edit page they were on and the form is redisplayed with helpful error messages. Indeed, this has become a standard Rails pattern.
Which brings us to my most-recent roadblock. MongoDB is my new favorite toy and MongoMapper is a promising stand-in for ActiveRecord. However, when I employed this standard Rails pattern:
def new
@user = flash[:user] || User.new
end
def create
@user = User.new params[:user]
if @user.save
redirect_to user_path(@user)
else
flash[:user] = @user
redirect_to new_user_path
end
end
I was stymied by this error:
TypeError: singleton can't be dumped
Some chain of references from the model evidently connects it to a singleton object. MongoMapper is still under heavy development, so I'm confident at some point this will be resolved, but meanwhile the following monkey patch appears to do the trick: module MongoMapper
module Document
def marshal_dump
instance_variable_names.inject({}) { |m, name| m[name] = instance_variable_get(name); m }
end
def marshal_load(values)
values.each_pair { |k, v| instance_variable_set(k, v) }
end
end
end
I stuck this code in a file called mongo_marshalling.rb in config/initializers. 