0

so i have made several attempts at this, but decided i needed some advice from the community regarding best practices. here is what i am trying to do.

i have multiple elements in multiple views that i am using jquery toggles to show or hide them. what i want to do is save the state of them so they are persistant accross requests. (ie, if a user has panel1 showing, but panel2 hidden, when they refresh, they remain that way)

i was thinking each time a user toggles an element, i could save that element's css display value in a session. then after each page load, i could use jquery's .class selector to get all the elements with a class of say 'toggle' and apply that to those elements.

the idea seems sound, but i am not savvy enough to pull it off. i am not sure how to execute the javascript, and have it parse the ruby code, other than having an ajax call a .js.erb for that view.

i have played around with it for hours, with no success. does anyone have any suggestions that would get me started in the right direction?

2 Answers 2

1

Perhaps a simpler approach would be to use cookies to save the state of your toggles. That could be done with jQuery/JavaScript and completely eliminate the need to have Rails worry about this.

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

1 Comment

sometimes you get so far down a rabbit hole that you forget there are other holes. this is a much simpler solution. thanks!
0

I think there are two ways to proceed.

1) You mention the first one which is very temporary: saving info into session or in the cache

2) If you want it persistent: Save the state of your current user in database and in your cache for fast access.

Both of them require some ajax request you have to handle to let your server know what's happening.

I'd use some jQuery functions: click then toggle which would add or remove a 'hidden' class (which would be a mere 'display:none;' in your css). I'd keep these functions on client side (not using any js.erj nor rjs).

In addition with the toggle, I'd send the ajax request with a boolean based on the presence or absence of 'hidden' class in the panel the user is clicking. This would only render :text => 'status saved' or 'status not saved'.

When the page loads the first time, simply add <%= 'hidden' if panel1_show == true %> to the class of each panel.

Hopes this helps,

Ben

1 Comment

since i don't really care if they are persistent across sessions, just across multiple page requests, i went with the cookie solution and saved myself from making any ajax requests entirely.

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.