对于与get不同的请求,params哈希

时间:2016-12-17 19:10:13

标签: ruby-on-rails parameters

在网址application.co/users/2我有一个表单,用于删除current_useruser 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教程中的link1link2

1 个答案:

答案 0 :(得分:1)

因为您使用form_for类而不是Relationship类的对象初始化User

返回Relationship对象

current_user.active_relationships.find_by(followed_id: @user.id)