I'm worried about implementing the follow function ...
I can unfollow like this, but I'm in trouble because I can't follow ...
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 ...
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
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
Related articles
- ruby on rails 6 - [error] nomethoderror in followscontroller # create undefined method `follows' for nil: nilclass
- ruby on rails - unknown action error when trying to post a comment
- ruby on rails - rspec validation error
- ruby - an error occurred when implementing the micropost search function using ransack
- ruby on rails - rails routing error
- ruby on rails - nomethoderror in itemscontroller # new error
- ruby on rails - syntax error cannot be resolved
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails - i get an error without being created
- ruby on rails - [rails6] actioncable data transfer does not work
- ruby on rails 6 - when i press new user registration, i get an error of no route matches [get] "/ users"
- ruby on rails - i want to resolve rails name error
- ruby on rails - rails6 rspec integration test error
- ruby on rails - when i run rails, i get an error related to webpacker
- ruby - an error occurs when building a rails 6 environment with docker
- ruby on rails - bootstap 4 does not work in rails6 production environment
- solution for ruby on rails routes error
- ruby on rails - i want to download a file with rails, s3 and carrierwave, but i get an error
- ruby on rails - after updating to rails6
- php : How to take a screenshot of a website page?
- javascript : How to add +1 to the numerical content of the HTML element?
- javascript : I can not connect php to js
- Convert JSON string to an object received from ajax in jQuery
- jquery : The function caused from the collbek does not work Angular 12
- JQuery.Validate () Does Getting A Request
- javascript : Ajax gives undefined, although through the log -everything is ok
- javascript : Does not go redirection when authorized
- php : Sending data via AJAX
- php : After sending the AJAX request reckoning to the handler page
It is said that id does not have a required key here.
Checking routes.rb, it seems that user_id and id are required as arguments. In other words, you didn't have this ID.
Originally I would like to pass the ID of RelationShip to id, but for example, how about entering an appropriate value as follows?