0

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.

Amr Adel
  • 574
  • 1
  • 7
  • 18
smiley
  • 1
  • 1
  • Did you tried to search for this before? There are many answers for this, [link](https://stackoverflow.com/questions/8225518/calling-a-method-from-another-controller), [another](https://stackoverflow.com/questions/37750087/calling-a-method-of-another-controller), But i agree with the first link in not to make an instance of a controller, You can use redirect_to and send data like this [link](https://stackoverflow.com/questions/1430247/passing-parameters-in-rails-redirect-to), Hope it helps. – Amr Adel Jan 18 '18 at 08:33
  • hello Amr Adel thank you for ur reply, yes i saw those solutions but the problem is that the redirect don't work unless its a GET request. and i need for get and post requests. what do you think the best practice can be? – smiley Jan 18 '18 at 09:15
  • I would recommend creating service objects as models instead: [link]https://www.engineyard.com/blog/keeping-your-rails-controllers-dry-with-services – GabrieleF Jan 18 '18 at 10:30
  • I'm sorry for my late response, but i totally agree with @GabrieleF, Modeling things gonna be safer and more organized, If you have to make it in the controller, I think redirect_to with params is the best thing, This is my opinion. – Amr Adel Jan 18 '18 at 10:44

0 Answers0