views:

50

answers:

2

Hi All,

I have a simple (yet somehow convoluted) issue. Basically I'm adding items to make my web app more "desktop-like". For instance, right now I'm trying to get a page to dynamically load info into a DIV based on previously selected items. I'm currently using a cookie to handle saving the data, but I can't for the life of me get my brain to work this problem out.

I have a scenario with the following relationships:

SITE has_many BUILDINGS

BUILDING has_many METERS

METER

All entities can have associated charts. So, in an effort to make it generic, I set up a "has_many" relationship for each to CHARTS and abstracted it like so.

SITE has_many CHARTS, as chartable

BUILDING has_many CHARTS, as chartable

METER has_many CHARTS, as chartable

Once the user selects an item from the menu on the left, I then use a method to determine what item needs charts found and I display the particular item's charts. That all works fine.

My issue now is working with cookies in order to either save data to independent keys (or perhaps Marshal objects) in order to dynamically reload the previously selected item's data whenever the page reloads. The ajax call requires several values in order for the "update" action to find the correct item and display it. I'm having trouble with whether to use Javascript directly, try to trigger an action, or use some kind of combination.

As I said, I'm sure the issue is rather simple or straightforward, but I'm just not seeing it. If this description is a bit vague, I do apologize. Feel free to ask for more info.

Best

A: 

This is too big for storing in cookies, you should either:

  • Store an id cookie client-side and store the data on the server-side which can be accessed with a corresponding id cookie and valid authentication credentials.
  • Use HTML5 client-side storage such as localStorage or a local database.
Eli Grey
+1  A: 

When the user selects an item from the menu, save all the necessary information to re-select that item to a cookie. Bind a Javascript method to the page load and check the value of that cookie. If the information is there indicating that an item should be preselected, just call the same Javascript method that is called when the user selects a new item from the menu. If you're using JQuery, for example, you might do something like this to bind to the page load:

$(document).ready(function() { /* check cookie and do stuff */ }

Another thing you could do is pre-render that stuff in your RoR code if that cookie exists so you don't immediately execute an AJAX call on page load (since that is sometimes considered bad form due to the page load performance hit).

Marc W
Well, currently the "update_div" action that's part of my controller (I know, bad) handles everything and updates the div. I simply have a "link_to" that calls it. Perhaps I should check the cookie inside the action? I've boiled the method down a bit. Now the only data I POST to the action is as follows: chartable_type, chartable_id, title. With that data it acquires the chartable item and renders the new html based on it.As a bit of a side question, how would I trigger the RoR action on page load?
humble_coder