由于 Postgres 无法取消关注用户

编程入门 行业动态 更新时间:2024-10-23 01:31:28
本文介绍了由于 Postgres 无法取消关注用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Rails 3.2,推特应用

Rails 3.2, Twitter App

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

UPDATE: Sovled but any idea why it might work once, then when i try it again, I get Unknown key: #<User:0x007f9a5a946708> On line 16 users_controller.rb for @user. If i update a status or re-login its fine.

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

SOLUTION: After adding a notice: "Added", redirect_to_path under each if/else it worked fine. I haven't been able to produce an error for "user not found" tho.

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

I got a form_for, I type in :username, it follows or unfollows. Unfollow don't work. Looks like this.

错误

PG::UndefinedTable:错误:缺少表id"的 FROM 子句条目第 1 行:从关系"中删除id".follower_id"= 1 和...^: DELETE FROM "relationships" WHERE "id"."follower_id" = 1 AND "id"."followed_id" = 2

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

id is missing? So I think the problem is in 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..那里需要改变什么?

So that's a lot of words, but look in Relationship.delete.. what needs to change there?

推荐答案

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

Relationship.delete is expecting an id. Try something like:

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

或者,也许更清楚一点,如果后面是关系:

Or, perhaps a bit clearer, if followeds are relationships:

def unfollow user self.followeds.where(:followed_id => user.id).first.delete end #didn't work for @ladiesman217

更多推荐

由于 Postgres 无法取消关注用户

本文发布于:2023-10-16 16:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   Postgres

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!