Pasar modelo no guardado para ver

I have a model "Users" and am working with a Session.

Users, without being logged in, are allowed to modify a User.new object in the session, without actually saving the User.

I would like to enable the not-yet-logged-in user to view their Users#Show page.

¿Cuál es la mejor manera de hacer esto?

¡Gracias!

preguntado el 03 de mayo de 12 a las 16:05

2 Respuestas

In your controller, you could do something like this:

def show
  @user = current_user || session[:current_user]
end

So you get the current_user if there is one, and if not, you read from the session.

Storing an entire model in the session usually sucks though. If you change the underlying class, then the object in the session won't be unmarshalled correctly, so ensure you reset your session with reset_session after you make changes to your User class or else you'll get lots of strange errors.

contestado el 03 de mayo de 12 a las 16:05

The answer above works. I was looking for the "Session Based Model" pattern. There is a Railscast on Session Based Models that I found to be very helpful for working with this pattern.

contestado el 04 de mayo de 12 a las 20:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.