Ruby 关于 Rails 最喜欢的控制器和语法问题

Ruby on Rails favoriting controller & syntax problems

我正在 favorites_controller 中创建一个 "destroy" 方法。不确定我应该如何表达下面注释掉的要求,要求使用参数中的 ID 找到当前用户的最爱。我知道我很接近,但我目前对 ruby 语法的了解还没有达到标准。当我使用我的应用程序的不喜欢的功能时,弹出 "undefined method `favorite' for #" 错误并指向下面的 [initialized?] 'favorite' 变量行。请帮忙。谢谢!

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

我假设用户可以有很多收藏夹,因为您正试图通过 params[:id] 挑出一个。如果是这种情况,请尝试将 .favorite 更改为 .favorites

希望能解决问题![​​=13=]

我猜用户和收藏夹之间的关系是 has_many 所以在这种情况下你应该使用 current_user.favorites