Matt White
Matt White

Reputation: 117

Updating model attributes in Ruby

I am attempting to enable a user to change the status of incoming friend requests from "unaccepted" to "accepted"

User Model

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :places


has_many :sent_friendships, class_name: "Friendship" , foreign_key: :sender_id
has_many :received_friendships, class_name: "Friendship", foreign_key: :receiver_id

Friendship Model

class Friendship < ActiveRecord::Base
belongs_to :sender, class_name: "User"
belongs_to :receiver, class_name: "User"
end

User Show View

    <h1>Incoming Friend Requests</h1>
<% current_user.received_friendships.each do |friendship| %>
<%= friendship.sender.name %>
<%= link_to "Accept", user_accept_path(current_user), class: 'btn btn-success', method: :post %>
<% end %>

Friendships Controller

 class FriendshipsController < ApplicationController

def accept
@user = User.find(params[:user_id])



end

end

Where is where I am stumbling because I am unsure as to how best to plus in the sender id and receiver id to update the proper line on the Friendship model table.

Any help is appreciated.

Upvotes: 0

Views: 39

Answers (2)

zmii
zmii

Reputation: 4307

As every your controller has access to current_user variable, you may not pass it as parameter, but you should pass a friendship.sender to it.

Then depending on you routes and architecture you may do one of the following:

  1. Create a new Frienship

    class FriendshipsController < ApplicationController

    def accept
        @user = User.find(params[:id])
        friendship.create sender_id: current_user.id, reciever_id: @user.id
        # or
        # friendship.create sender_id: current_user.id, reciever_id: params[:id]
    
        # then rdictect to somewhere
        redirect ...
    end
    

    end

  2. If you have additional fields in the Frienship model like - accepted, you may update. Then you find a created Frienship model during send of friendship invitation and update the field.

    class FriendshipsController < ApplicationController

    def accept
        @user = User.find(params[:id])
        friendship = Frienship.where("sender_id = ? and reciever_id = ?",
                                 # sender was passed as an argument to link_to in the view
                                    @user.id, current_user.id)
        friendship.accept = true
        friendship.save!
    
    
        # then rdictect to somewhere
        redirect ...
    end
    

    end

Upvotes: 1

twonegatives
twonegatives

Reputation: 3458

Your accept method should basically do a set of things:

  1. Obtain a user who accepts the request (you should already have this done for you as devise has current_user helper method accessible in controller)
  2. Obtain a corresponding received_friendship to know which particular user is going to be a new friend for current_user
  3. Update the friendship object (from step #2) to have acceptance (or whatever you named it) attribute set to accepted.

As you already know, first step does not require any coding from you. The second one should be as simple as setting a local (or @instance) variable based on sender_id of friendship request (or id of particular friendship, which would be even better). And the last one would look something like

friendship.acceptance = "accepted"
if friendship.save
  # TODO: redirect to somewhere with a successful notice
else
  # TODO: render users/show template once again with an error
end

One more hint: note that your acceptance link does not pass data required at step #2:

<%= link_to "Accept", user_accept_path(current_user) %>

Upvotes: 1

Related Questions