I am trying to build an api only application using ruby on rails, for the authentication i am using the has_secure_password for my User model. So in my application there are two types of users one is admin one is guest, an admin is able to deactivate a guest user, the problem is when i call a put request to deactivate a user.
I get this error Password can't be blank
. My problem is very similar to this Question rather it is more of follow up for this.
I believe the issue is that the has_secure_password is expecting me to provide as password in the params/body but for deactivating the guest user there's no need for the admin to know the password for that user right? an id is enough in that case
My url for the put query is this "/api/v1/users/:id/deactivate(.:format)" where in the :id params i just give the id for the guest which i want to deactivate
Here is the user model
class User < ApplicationRecord
has_secure_password
before_create :generate_token
has_many :registrations
has_many :organized_events, class_name: 'Event', foreign_key: 'organizer_id'
has_many :attended_events, through: :registrations, source: :event
validates :username, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true,
format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
validates_presence_of :first_name, :password
enum role: { admin: 0, guest: 1 }
scope :active, -> { where active: true }
private def generate_token
self.authentication_token ||= SecureRandom.hex(6)
end
end
And here is the code block where I'm trying to make the request to deactivate request
def deactivate(id)
user = User.find_by(id: id)
if user.blank?
self.errors = "User not found"
else
self.errors = user.errors.full_messages unless user.update(active: false)
end
end
I'm very much stuck not sure how to proceed from here on