Rails 3.2,Twitter App
更新:已经知道了,但任何想法为什么它可以工作一次,然后当我再次尝试时,我得到Unknown key: #<User:0x007f9a5a946708>
第16行users_controller.rb
为@user。如果我更新状态或重新登录其罚款。
解决方案:在每个if / else下添加notice: "Added", redirect_to_path
后,它运行正常。我无法为“用户未找到”而产生错误。
我收到了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
id丢失了?所以我认为问题出在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 ..那里需要改变什么?
答案 0 :(得分: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