Home>

I'm worried about implementing the follow function ...


I can unfollow like this, but I'm in trouble because I can't follow ...

Error statement
ActionView :: Template :: Error (No route matches {: action =>"destroy",: controller =>"relationships",: user_id =>#<User id: 1, nickname: "for guests", email: "guest" @ example.com ", created_at:" 2020-10-23 13:14:23 ", updated_at:" 2020-10-23 13:14:23 ">},

 missing required keys: [: id]):
    1:<!-Unfollow button ----------------------------------------- ------------------------->  2:<% = form_for (current_user, url: user_relationship_path (@user), method :: delete, remote: true) do | f |%>  3:<% = f.submit "Unfollow", class: "btn btn-outline-secondary"%>  4:<% end%>


The error part is here. ..

missing required keys: [: id]
I understand that there is no ID column for "relationships" that manages follow-up etc.

When I reload the screen, the follow information disappears or is added from the database firmly, so I assume that there is a problem with Ajax processing

It will be longer from here, but I will attach the code
Please help us ...

Description

I will look from the view

app/views/users/show.html.erb<% = render "follow_form"%>


The contents of this render is ↓

app/views/_follow_form.html.erb<% if user_signed_in?&&@user! = current_user%>          <% if current_user.following? (@User)%>      <% = render "unfollow"%>#Call render if i am following
      <% else%>      <% = render "follow"%>#Call this render if you haven't followed
      <% end%>      <% end%>

This error does not switch the screen after pressing "Follow", so I think there is a problem with follow, so I will look at<% = render "follow"%>

app/views/users/_follow.html.erb<% = form_for (current_user, url: user_relationships_path (@user), method :: post, remote: true) do | f |%><% = hidden_field_tag: following_id, @ user.id%><% = f.submit "follow", class: "btn btn-outline-secondary"%><% end%>


remote: true and sending in JS format

I think there is a problem with the controller or model ...

Controller description

app/controllers/relationships_controller.rb
def create
    @user = User.find (params [: following_id])
    current_user.follow (@user)
  end

Model description

app/model/user.rb
 def follow (user)
    following_relationships.create! (following_id: user.id)
  end


routing

config/routes.rb
                   user_relationships POST/users /: user_id/relationships (.: format) relationships # create
                    user_relationship DELETE/users /: user_id/relationships /: id (.: format) relationships # destroy

Received in create.js.erb

app/views/relief/create.js.erb
$("# follow_form"). html ("<% = j (render ("users/unfollow "))%>");

Go to user/unfollow with<% = j (render ("users/unfollow"))%>

app/views/relationships/create.js.erb
$("# follow_form"). html ("<% = j (render ("users/unfollow "))%>");

<% = j (Look at users/unfollow in render ("users/unfollow"

app/views/user/_unfollow.html.erb<!-Unfollow button ------------------------------------------- -----------------------><% = form_for (current_user, url: user_relationship_path (@user), method:: delete, remote: true) do | f |%><% = f.submit "Unfollow", class: "btn btn-outline-secondary"%><% end%>

It is written like this
Please lend us your help ... (;_;)

  • Answer # 1

    It is said that id does not have a required key here.

    missing required keys: [: id]):
        1:<!-Unfollow button ----------------------------------------- ------------------------->
        2:<% = form_for (current_user, url: user_relationship_path (@user), method :: delete, remote: true) do | f |%>


    Checking routes.rb, it seems that user_id and id are required as arguments. In other words, you didn't have this ID.

    user_relationship DELETE/users /: user_id/relationships /: id (.: format)

    Originally I would like to pass the ID of RelationShip to id, but for example, how about entering an appropriate value as follows?

    user_relationship_path (user_id: @user, id: 1)