views:

31

answers:

2

This will probably be easiest if I explain what I'm trying to do. I have three actions in my Rails app controller, each rendering a different page. The page-render is done with a single partial which uses variables that were set in the controller action code. For example, each page has a list on it, but on one page the list is sortable. Up to now I've been handling this by setting a @sortable flag to true or false in the code for my actions.

This works fine when an action is initially run. The problem is that I have AJAX stuff going on (e.g. adding a new element to the list) and when this happens, I need to know the value of the @sortable variable again. It seems to have gone, even though I'm still technically on the same page. What I want is a variable store that is linked to the page you are on.

What are your recommendations for doing this? (Storing it in the Rails session hash seems like overkill - too much chance that the wrong value will get left in there by some yet-to-be-implemented action.)

Ben

+1  A: 

Why do you don't want use session? As for me before_filter works fine for such tasks

in ApplicationController

before_filter :init_actions

def init_actions
  session[:action] = action_name
  session[:controller] = controller_name
end
Bohdan Pohorilets
I had forgotten about before_filter - that way I could make sure it always gets set regardless of the action. My problem with session is more one of principle: these variables aren't session-scope, they're page-scope (or action-scope if you like).
Ben
If you don't want to use sessions just save them as variables and you can acces them in your wievsdef init_actions @current_action = action_name @current_controller = controller_nameend
Bohdan Pohorilets
@Bohdan - that won't help me. The scope of the variables is the same as that of the controller they reside in, and the controller instance has 'gone away' once my page has rendered in my browser. Subequent AJAX calls have no access to this controller.
Ben
Just to close this one off I actually ran for a while using the session hash. It's not robust for my use though, because if you hit the back button on the browser the session is unaffected, even though I might now be back in a different action. For my AJAX calls to then work I need to have stored something on the rendered page, as suggested in the accepted answer.
Ben
+1  A: 

In rails I've only managed to set page scoped variables for initial setup too.

I think the only solution would be to pass the sortable flag from the page on the ajax request. You can store it either with a javascript variable, in a hidden field, custom attribute on your list or anyway you wish and then in the ajax you simply add that to the request so you can treat that on the server side persistently.

Angelus
Hmm, yes, seems like there could be a be better way to do this though - know what I mean? At the moment I'm experimenting with putting them in the `flash`, which seems to be working so far - I just have to watch that they don't get printed out in the layout file!
Ben
After doing plenty of digging around on this one, I think you're right Angelus. Going to have to bite the bullet.
Ben