[英]Ruby on Rails favoriting controller & syntax problems
I'm creating a "destroy" method in my favorites_controller. 我正在我的收藏夹控制器中创建一个“销毁”方法。 Not sure how I should express the commented-out requirement below, asking to find the current user's favorite with the ID in the params.
不知道我该如何表达以下注释掉的要求,要求使用参数中的ID查找当前用户的收藏夹。 I know I'm close but my current knowledge of ruby syntax is not up to par yet.
我知道我已经接近了,但是我目前对ruby语法的了解还没有达到标准。 An "undefined method `favorite' for #" error pops up and points to the [initialized?] 'favorite' variable line below when I use the unfavorite function of my app.
当我使用我的应用程序的收藏夹功能时,会弹出“未定义#方法的收藏夹”的错误,并指向下面的[initialized?]“收藏夹”变量行。 Please help.
请帮忙。 Thank you!
谢谢!
def destroy
# Get the post from the params
@post = Post.find(params[:post_id])
# Find the current user's favorite with the ID in the params
favorite = current_user.favorite.find(params[:id])
if favorite.destroy
flash[:notice] = "Post was succesfully un-favorited"
redirect_to @post
# Flash success and redirect to @post
else
flash[:error] = "There was an error in un-favoriting this post"
redirect_to @post
# Flash error and redirect to @post
end
end
I'm assuming the User can have many Favorites, since you're attempting to single one out via params[:id]
. 我假设用户可以有很多收藏夹,因为您正试图通过
params[:id]
。 If that's the case, try changing .favorite
to .favorites
. 如果是这种情况,请尝试将
.favorite
更改为.favorites
。
Hope that solves it! 希望能解决!
我猜用户和收藏夹之间的关系是has_many,因此在这种情况下,您应该使用current_user.favorites
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.