我的主要困难来自于理解 the _followand _unfollowpartials 与第 11.2.3 章中定义的and方法create之间destroy的关系。我现在只关注取消关注用户的行为(因为关注的行为大多是类似的)。RelationshipsController
Hartl 将部分 for 定义unfollow为:
<%= form_for(current_user.relationships.find_by_followed_id(@user), html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn btn-large" %>
<% end %>
以及相应的destroy操作:
def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
redirect_to @user
end
我难以理解的是:
- 部分 ..
@user第一行中的变量是a)在当前显示页面的操作中定义的,还是b)在操作中定义的?看来helper 已经找到要销毁的了,那为什么action 还需要在控制器中重新找到要销毁的呢?unfollowshowdestroyform_for@userdestroy@user - 在该
destroy方法中,@user首先找到Relationship id。我没有看到关系 id 首先是如何传递到 URI 中的(因为看到要取消关注的特定用户显示为/users/2),更不用说它是如何用于查找@user要销毁的了。我知道每个关系表都有一个 id、一个 follower_id 和一个 follower_id,但看不到 id 元素本身是如何在这里发挥作用的。
感谢您阅读并回答我的问题!