0

In my domain, a user chooses a Universe to edit, then can view/edit/destroy etc. models associated with the Universe they choose. Basically, the entire website depends on which Universe you are currently viewing.

I had just been passing which Universe in the params, but this is quickly getting untractable. Is there a rails standard way to keep track of what the last Universe a user selected was? Should I store this information in some sort of cookie? Should I make some sort of weird singleton model to keep the data in? Is there way to use the Session (like you would for keeping track of which user is logged in)?

6
  • 3
    Yes you can use session, like we usually use the session to store the user_id, you can store the universe_id: session[:universe_id] = Universe.current (see also Thread.current[:variable] to store variable accross the whole app, but is deprecated) Commented Sep 12, 2013 at 13:29
  • Then I would (in every controller) say something like @universe = session[:universe_id]? Commented Sep 12, 2013 at 13:31
  • It isn't a bad solution, putting this is the ApplicationController with a before_filter can solve nicely. Commented Sep 12, 2013 at 13:39
  • @Jenny Yes, but you might want to introduce a helper method to provide a layer of indirection (e.g. current_universe) in case you change your mind in the future. Commented Sep 12, 2013 at 13:42
  • In implementing this solution (before filters, the whole nine yards) it seems like the session is not persisiting between javascript ajax submissions and normal html ones. I seem to recall this being a problem. Is there anyway to use the session in this case? Commented Sep 12, 2013 at 13:51

1 Answer 1

3

How are your routes set up? Use nested routes for any dependencies of that sort:

resources :universes do
   resources :planets
   resources :spaceships
end

The urls to your pages then look like this:

www.somedomain.com/:universe_id/planets/:planet_id/edit?param=1&param2=2

Then you always have the universe id available and you can use a before_filter in your ApplicationController to call a function which loads the universe before any action, so you always have the @universe available without you having any work.

Of course you still have to add the universe to your links but that can also be automated by creating helper methods which makes use of your always present @universe object. The default helpers created for these resource routes look like edit_universe_planet_path(@universe, planet.id)

Sign up to request clarification or add additional context in comments.

5 Comments

I thought this was what the OP wanted to get away from. Did you read the question differently?
If I can avoid hunting down every single link I already have to modify it to work (I originally designed the system without universes), I'd like to. Storing it in the session seems to be working. Is there a problem with this solution?
Well I thought the process of managing the ID can be optimized. Storing things into the session always seems like the easiest and most convenient idea but I prefer to steer clear of that as much as possible. Storing a simple selection just to have it available on every page smells like cookie abuse. What if the user doesn't accept cookies? What if it somehow changes because the user has two tabs of the application with two different universes selected?
Multiple tabs break session vars...hrm...that IS a problem. Is there an easy way to find all my links, or am I going to have to open a few dozen pages up?
You would have to replace your links regardless of the solution to your problem, right? Hm, depends on how consistent you were at building links. You might find most links by searching for 'link_to' or 'redirect_to', maybe '*_path' if you made use of resourceful routes. You probably cannot replace them automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.