2016-01-14 103 views
0

我有一个模型友谊:需要理解为什么:随机缺少必需的键:[:ID]错误

belongs_to :user 
belongs_to :friend, class_name: 'User' 

和用户模型:

has_many :friendships 
has_many :friends, through: :friendships 

用户控制器:

@user = User.find_by!(username: params[:username]) 
@user_following = @user.friends.all 
@user_followers = @user.inverse_friends.all 

说用户A加为好友用户B

则用户A添加为好友用户C

用户B查看用户A

Adding and Destroying friendships works. 

我得到一个错误,任何人都可以解释为什么发生这种情况? 误差消失时用户B添加用户C

它指出该动作:

<%= button_to "following", friendship_path(current_user.friendships.find_by(friend_id:following.id)), method: :delete %> 

没有路由匹配{:行动=> “消灭”,:控制器=> “友谊”, :id => nil}缺少必需的钥匙:[:id]

回答

1

找不到友谊。我的建议是处理这种情况下类似:

<% if friendship = current_user.friendships.find_by(friend_id: following.id) %> 
    <%= button_to "following", friendship_path(friendship), method: :delete %> 
<% else %> 
    <%= "No friendship found" %> 
<% end %> 
+0

嘿伙计,感谢您的信息。这个问题似乎消失了,但需要更多的测试。生病接受你的答案。谢谢 – mrvncaragay

相关问题