0

I'm trying to make changes made by javascript persistent. Specifically, I have a rails app which allows a user to "like" other users' posts. When the user likes (faves) a post, or when a new favorite is created, I want the background colour of the post to change (say, to green) using javascript and/or jQuery (I could not see any other way to do this). The problem is that when I reload the page the change is not persistent, with the background colour turning into the default one.

I've tried the following, none of which worked;

addClass in combination with removeClass

append the <style> ... </style> tags directly so the changes in styling can be persistent, without success.

I've tried the above both in app/views/favorites/create.js.erb and directly in the view template where faved posts appear using <script> ... </script> tag.

Any suggestions will be greatly appreciated.

4
  • 1
    Are you successfully saving the favorite (or something like it) to the database when the user likes a post? Commented May 5, 2016 at 13:45
  • for me what jvillian asks is the crux of the situation. I am personally loathe to store any view specific stuff into the database, but if you are persisting the concept of a 'favorite' perhaps you can switch UI behavior based on that state? Commented May 5, 2016 at 13:51
  • I have favorite model stored in the database and I use Ajax request to create/destroy the resource. What I wanted was just a client-side logic which could allow modifications made by javascript to be persistent. But since this seems to require a storing of UI data in the db, which you say is not the right approach, I decided not to implement this feature. Could you tell me why it's a bad idea to store UI information in the db? Commented May 5, 2016 at 16:08
  • @humblearner I've updated my answer with an example. You could get an idea. Commented May 6, 2016 at 2:13

1 Answer 1

2

For data to be persistent, you need to store it in database. You can make an ajax request and save the like transaction in the db. On successful ajax request, you can modify elements using addClass or removeClass. These changes will be lost on reload.

Depending upon the saved value in db, you need to assign the class in the front end. The same element modifications you have to write in erb file.

<div class=<%= liked?(post, current_user) ? 'liked-class' : 'not-liked-class' %>>
</div>

You need something like this. The above code is just an example. You need to write the liked? helper method logic, class names etc.

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

3 Comments

For me, saving UI information in the db is a 'smell'. The exception for me would be if what is saved represents a conceptual state of business logic value. The concept of 'favorites' seems to be valid database information, while saving the more SQL-esque transactional information feels wrong to me. Just an opinion, which is why I'm only placing it here as a comment
Yes, yes. I am not suggesting to save ui information in db. Just the like or favorite data. Database seems to be the right place for such data.
Ah, I totally misunderstood 'like transaction'. I was answering another question somewhere around ActiveRecord::Transaction so my brain was in that space already. I agree with your point then. thanks for the clarification.

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.