All threads / Liking Posts | GoRails - GoRails
Ask A Question


You’re not receiving notifications from this thread.

Liking Posts | GoRails - GoRails

Chris Oliver asked in General

Hi Chris - I'm trying to build out the likes_controller, but I got an error related to friendly_id. I've implemented friendly_id on the Post model, could you please shed some light on solving this issue?

NoMethodError in Posts::LikesController#create
undefined method `slug' for #<like:0x007ff7c4d231b0>

before action :set_post

def set_post
@post = Post.friendly.find(params[:id])

Have you added the slug attribute to the Like model? That's a requirement to make friendly_id work. May want to read their directions again to get familiar with it.

yes. that's exactly what's missing. Thank you!

I came across an issue when I tried to "like" a post without signing in, I'm not sure if "authenticate_user!" in the likes_controller is invoked because of the AJAX response....

When you're not signed in, I render the like button, but it takes you to sign in instead of the AJAX code there. Tweaked that a bit because the AJAX code won't do the redirect with UJS properly.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 69,840+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.