在网址application.co/users/2
我有一个表单,用于删除current_user
和user 2
之间的关系。表单的代码是:
<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id), html: { method: :delete }) do |f| %>
<%= f.submit "Unfollow", class: "btn" %>
<% end %>
关系控制器的销毁操作是:
def destroy
user = Relationship.find(params[:id]).followed
current_user.unfollow(user)
redirect_to user
end
为什么行动依赖于假设params[:id]
代表关系的id?我认为params[:id]
代表网址中的数字2。
请参阅Michael Hartl教程中的link1和link2。
答案 0 :(得分:1)
因为您使用form_for
类而不是Relationship
类的对象初始化User
。
返回Relationship
对象
current_user.active_relationships.find_by(followed_id: @user.id)