TenJack
TenJack

Reputation: 1632

Render w Params or Redirect w Errors in Ruby on Rails

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?

Upvotes: 2

Views: 816

Answers (2)

Taryn East
Taryn East

Reputation: 27747

"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.

Upvotes: 0

Toby Hede
Toby Hede

Reputation: 37143

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).

Upvotes: 2

Related Questions