I was recently coding a project in RoR framework. But i got stuck into a problem. In my controllers i have a controller called Api controller which i use to make Api calls that are apis outside Rails, and they work fine. But i created some other functions in other controllers which i want to use them like an Api but idk how to do it
Api function to make the API calls:
def show_api_data
@apis = Api.load_by_api_id(load_api_data_params[:api_id], load_api_data_params[:callparams], current_user)
return render json: @apis, status: @apis[:errors][0][:status] if @apis[:errors].present?
render json: @apis, fields: { apis: %i[api_data total_count] }
end
def post_api_data
@apis = Api.load_by_api_id(load_api_data_params[:api_id], load_api_data_params[:callparams], current_user)
return render json: @apis, status: @apis[:errors][0][:status] if @apis[:errors].present?
render json: @apis, fields: { apis: %i[api_data total_count] }
end
And this 2 functions from another functions for give roles to a user:
def add_role_to_user
role_params[:user_id].each do |user|
user = User.find(user)
role_params[:role_ids].each do |role|
@roles = Role.find(role)
if @roles.resource.present?
new_role = user.add_role(@roles.name, @roles.resource)
new_role.save!
end
end
end
render json: @roles
end
def remove_role_from_user
role_params[:user_id].each do |user|
user = User.find(user)
role_params[:role_ids].each do |role|
@roles = Role.find(role)
user.remove_role(@roles.name, @roles.resource) if @roles.resource.present?
end
end
render json: @roles
end
so what i'm trying to do is use this role functions as an API and send it to the api call functions also i'm using MongoDB.