Ruby on Rails favoriting controller & syntax problems -
i'm creating "destroy" method in favorites_controller. not sure how should express commented-out requirement below, asking find current user's favorite id in params. know i'm close current knowledge of ruby syntax not par yet. "undefined method `favorite' #" error pops , points [initialized?] 'favorite' variable line below when use unfavorite function of app. please help. thank you!
def destroy # post params @post = post.find(params[:post_id]) # find current user's favorite id in params favorite = current_user.favorite.find(params[:id]) if favorite.destroy flash[:notice] = "post succesfully un-favorited" redirect_to @post # flash success , redirect @post else flash[:error] = "there error in un-favoriting post" redirect_to @post # flash error , redirect @post end end
i'm assuming user can have many favorites, since you're attempting single 1 out via params[:id]
. if that's case, try changing .favorite
.favorites
.
hope solves it!
Comments
Post a Comment