I have a product, user, and like model. A user can like a product. I am trying to implement a simple like button which, upon a click, allows a user to like a product. Then the like button transforms into an unlike button, allowing the user to unlike a product. Pretty straightforward.
I have implemented the model/controller logic for the above. I am not very good with javascript/ajax and was wondering what the best way would be to implement the above functionality. I want the like/unlike actions to be handled via ajax. I am using rails 3 and jquery ujs for my app.
Thanks.
Basically, to make your links work via AJAX, you just need to provide
link_to
with:remote => true
argument. But I guess you want to make some sort of feedback to the user. Take a look at this screencast.My answer to your question is long, so I wrote up an example application. Here's a snippet:
There are many ways to skin this cat, but I like to render a partial and a single ujs template.
_like_button.html.erb
:toggle.js.erb
, where"#like"
is the div enclosing the form: