views:

304

answers:

2

I am using ruby on rails and have things on my site that users can click to save and they are redirected to a page with both a login and a signup so that the user can continue with either option and save the content. The creates a problem for showing the proper user validation errors, b/c I need to use a redirect_to users/new in order to pass the params with the object id that the user is saving and if I use render :new, the errors are displayed but the object id is lost. Anyone have any solutions for this?

+1  A: 

Store the original item id in the session, proceed with your normal login/signup process, when that completes, if there is a save item in the session, redirect to the action that handles the save (it can now grab the item id from the session and proceed).

Toby Hede
just in case: probably not the item itself, but its id)
neutrino
Yep. Edited to reflect comment.
Toby Hede
yea, thats right. thanks
TenJack
Out of curiosity, what is wrong with saving the object itself in the session? That way I wouldn't have to perform a second database lookup to find the object again.
TenJack
Also, what about putting the session value into a user method that saves like user.save_with(session[:object]). This way I could put the object save, if it exists, inside a transaction with the user save.
TenJack
I think you will run into all sorts of concurrency problems if the object itself is in the session, not to mention it's always best to store the smallest amount of data possible.
Toby Hede
Not sure what you mean by using save_with, can you please clarify?
Toby Hede
A: 

"Out of curiosity, what is wrong with saving the object itself in the session? That way I wouldn't have to perform a second database lookup to find the object again." --TenJack (this should probably be a new StackOverflow question)

Saving an item in the session is a Bad Thing - because the moment you migrate your model object (eg to add a column or something similar), the data in the session is now no longer a valid object of the model's type. eg it will still have the old attribute-list instead of the new one... and it will appear as an invalid object.

This is why it's best to store only the id - because you will fetch a fresh, correctly instantiated object from the db.

Taryn East