0

Rails 3.2,推特应用

更新:已解决,但知道为什么它可能会工作一次,然后当我再次尝试时,我会Unknown key: #<User:0x007f9a5a946708>在第 16 行users_controller.rb获得@user。如果我更新状态或重新登录就可以了。

解决方案:在notice: "Added", redirect_to_path每个 if/else 下添加一个后,它工作正常。我无法为“找不到用户”产生错误。

我得到一个form_for,我输入:username,它跟随或取消关注。取消关注不起作用。看起来像这样。

Error

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "id" LINE 1: DELETE FROM "relationships" WHERE "id"."follower_id" = 1 AND... ^ : DELETE FROM "relationships" WHERE "id"."follower_id" = 1 AND "id"."followed_id" = 2

身份证不见了?所以我认为问题出在relationship_controller.rb

/users/buddies.html.erb

<%= form_for :username, :url => {:action => :buddies} do |f| %>
<%= f.text_field @user, placeholder: "username" %>
<%= f.submit "Add/Subtract" %>
<% end %>

users_controller.rb

@user = User.find_by_username(params[:username])

            if @user
                unless @user.blank?
                if current_user.following? @user
                    current_user.unfollow @user
                  else
                    current_user.follow @user
                end
              else
                flash[:error] = "stupid error";
                end
            end

user.rb

  def following? user
   self.followeds.include? user
  end

  def follow user
    Relationship.create follower_id: self.id, followed_id: user.id
  end

 def unfollow user
   Relationship.delete follower_id: self.id, followed_id: user.id
 end

relationships_controller.rb

def create

    @relationship = Relationship.new(params[:relationship]) 
    #@relationship.followed_id = params[:followed_id]
    @relationship.follower_id = current_user.id

    if @relationship.save
        redirect_to buddies_path, notice: "Phriend added"
        else
        flash[:error] = "Phriend not added";
        redirect_to buddies_path
    end
end

def delete
    @relationship = Relationship.find(params[:id])
    @relationship.delete
    redirect_to buddies_path, notice: "Phriend subtracted"
end

所以这是很多话,但看看Relationship.delete..那里需要改变什么?

4

1 回答 1

1

Relationship.delete 需要一个 id。尝试类似:

# app/models/user.rb
def unfollow user
  Relationship.where(:follower_id => self.id, :followed_id => user.id).first.delete
end

或者,也许更清楚一点,如果跟随的是关系:

def unfollow user
  self.followeds.where(:followed_id => user.id).first.delete
end
#didn't work for @ladiesman217
于 2013-11-08T01:35:33.190 回答