0

我有一个允许用户关注/取消关注其他用户的应用程序。此关系存储在relationships具有字段idfollower_id和的表中followed_id。当我调用该方法取消关注用户时,它会破坏关系,但随后会尝试使用已破坏的关系而不是 usersdestroy重定向回关注的用户。这是/曾经存储在关系表的字段中。我不知道如何在rails中解决这个问题。idididfollowed_id

这是关系控制器

class RelationshipsController < ApplicationController
    def create
        @relationship = Relationship.new
        @relationship.followed_id = params[:followed_id]
        @relationship.follower_id = current_user.id
        if @relationship.save
            redirect_to User.find params[:followed_id]
        else
            flash[:error] = "Couldn't Follow"
            redirect_to root_url
        end
    end

    def destroy
        @relationship = Relationship.find(params[:id])
        @relationship.destroy
        redirect_to user_path params[:id]
    end
end
4

2 回答 2

3

代替:

redirect_to user_path params[:id]

和:

redirect_to user_path(@relationship.followed_id)

@relationship已从 db 中删除,但内存中仍有对象。

于 2013-11-01T13:37:59.367 回答
1
def destroy
        @relationship = Relationship.find(params[:id])
        @followed_user_id = @relationship.followed_id
        @relationship.destroy
        redirect_to user_path @followed_user_id
    end

希望这可能会有所帮助:)

于 2013-11-01T13:45:56.370 回答