Asarluhi
Asarluhi

Reputation: 1290

params hash for requests different from get

At the url application.co/users/2 I have a form for deleting a relationship between current_user and user 2. The code for the form is:

<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id), html: { method: :delete }) do |f| %>
  <%= f.submit "Unfollow", class: "btn" %>
<% end %>

The destroy action of the Relationship controller is:

  def destroy
    user = Relationship.find(params[:id]).followed
    current_user.unfollow(user)
    redirect_to user
  end

Why does the action rely on the assumption that params[:id] represents the id of the relationship? I thought that params[:id] represented the number 2 in the url.
See link1 and link2 at the Michael Hartl's tutorial.

Upvotes: 0

Views: 26

Answers (1)

Alex Kojin
Alex Kojin

Reputation: 5204

Because you initialize form_for with object of Relationship class not User class.

There is returning Relationship object

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

Upvotes: 1

Related Questions