开发者

How to persist data about what page you are on in Rails app

开发者 https://www.devze.com 2023-01-08 23:15 出处:网络
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

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


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消