I have created an web app specified in Michael Hartl rails tutorial. In that the followers are directly follow any person. I need to upgrade it based on request and accept. Any links to solution or steps to the solution preferred.
Asked
Active
Viewed 616 times
1 Answers
2
You can generate a model called Request
, where you'll handle the connection between the users and also gives you control which user is sending the request and who's receiving the request. You can have fields like accepted_at
and declined_at
.
For example, the User A
can send a follow request to User B
. Based if the User B
accepted the request, you can send a notification to User A
, and make a connection between User A
and User B
.
In the model you can have something like:
class Request < ApplicationRecord
belongs_to :sender, class_name: "User", foreign_key: "sender_id" (or some other name like follower)
belongs_to :sending, class_name: "User", foreign_key: "sending_id" (or some other name like following)
end
In your form, you can have something like this:
<%= form_with model: @request, method: :put, local: true do |form| %>
<%= form.hidden_field :accepted_at, value: Time.now, type: "hidden" %>
<%= form.submit "Accept" %>
<% end %>
<%= form_with model: @request, method: :put, local: true do |form| %>
<%= form.hidden_field :declined_at, value: Time.now, type: "hidden" %>
<%= form.submit "Decline" %>
<% end %>
In the controller, you can have
def create
@request = Request.new(request_params)
if @request.save
flash[:notice] = "Follow invitation sent!"
redirect_to followers_path
else
flash[:alert] = "Errors. Couldn't send follow request"
render view_name
end
end
def update
@request = Request.find(params[:id])
@request.assign_attributes(request_params)
if @request.accepted_at_changed?
[here make the connection between users]
flash[:notice] = "You've accepted the follow request"
end
if @request.declined_at_changed?
[do nothing, just update the declined_at field]
flash[:notice] = "You've declined the follow request"
end
if @request.save
redirect_to followers_path
else
redirect_to root_path, alert: 'Not updated follow request!'
end
end
private
def request_params
params.require(:request).permit(:sender_id, :sending_id, :accepted_at, :declined_at)
end
I think this will give you a raw idea of how to do it. If you have some questions, I'll be glad to help.
Happy Coding.

Violeta
- 700
- 7
- 16
-
From where @request.accepted_at_changed model comes from – Mr.Movies Tamil Aug 30 '19 at 01:20
-
How can display the list of requested users? – Mr.Movies Tamil Aug 30 '19 at 01:21
-
Where do u learn this – Mr.Movies Tamil Aug 30 '19 at 02:06
-
For @request.accepted_at_changed?, you can read more here: https://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-changed-3F , also you can check here: https://stackoverflow.com/a/5051147/11883546 – Violeta Aug 30 '19 at 06:38
-
For displaying the pending requests of a user, you can create a model method that will find them. Then you can loop thru them something like ```pending_requests.where(accepted_at: nil, declined_at: nil)``` – Violeta Aug 30 '19 at 06:46
-
Once you provide more information, include more context and code samples in your description, we'll be able to help you more. This is something to start with. – Violeta Aug 30 '19 at 06:49