Unlike Button

Premium Content - Free Preview

Let's create an unlike button for users to unlike products. We'll start off in a similar manner as the like button, but then do things a little differently.

route and action

When a user unlikes a product, a like record is destroyed. Can you follow standard Rails practice to create a route and action for unliking a product?

The Code

Now let's fill in the controller code for destroying a like. This is simpler than creating a like, since we can just take in the id of the like and destroy it, we don't need to deal with any product. Can you fill in the destroy method so it destroys the like record with the given id?

Controller Code

setting up

Before we create the unlike button, we need to figure out how to display everything. Both buttons should only be shown to signed in users. The unlike button should only be shown to a user who likes a product, and the like button should only be shown to users who don't like a product. Can you setup these conditions on the product show page?

View Code

End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Contact Us
Sign in or email us at [email protected]