How to create a Ruby On Rails application (not just an API) and provide some routes as an API

Asked

Viewed 202 times

0

Hello, I am developing a common system with ROR and it has a structure not only of an API, as it has its views and everything else.

The point is that I will also build an API to serve it to a mobile app and when I search on the subject I only find people explaining how to build an application only as an api through the parameter:

rails new [my-app] --api

But this way the application needs different configurations and is much leaner than the normal application. I would like to use the following approach, follow a normal application and create a group of routes to serve the API, with authentication and everything.

How do I do that?

2 answers

1


You can run the rails new without the flag --api. So you keep Action View and everything.

Then you will have an API layer and an Asset Pipeline layer, the Rails standard. To create this API layer, create a folder in app/controllers/api and put your API controllers there. Also create a app/controllers/api/base_controller.rb, which will be the basis of all API controllers.

# app/controllers/api/base_controller.rb
class API::BaseController < ActionController::API
end

See that he inherits from ActionController::API, same as if you created the application with the flag --api, the difference is that you keep the ApplicationController.

Now, let’s create a controller.

# app/controllers/api/greetings_controller.rb
class API::GreetingsController < API::BaseController
  def index
    render json: { message: 'Hello World!' }, status: :ok
  end
end

And the routes:

# config/routes.rb
Rails.application.routes.draw do
  namespace :api do
    resources :greetings, only: :index
  end
end

And that’s it! You have Asset Pipeline running together with an API, scoped on API::.

  • Hello my friend, thank you, I will test this

  • Just an addendum, when I put for example API::Greetingscontroller there was an error stating that there was no such method, I had to replace it to Api::Greetingscontroller

1

You can create a namespace in his routes.rb and call it api for example:

namespace :api do
  resources :articles
end

Which will generate the following route table:

+-----------+--------------------------+------------------------+------------------------------+
|   HTTP    |           Path           |       Controller       |         Named Helper         |
+-----------+--------------------------+------------------------+------------------------------+
| GET       | /api/articles          | api/articles#index   | api_articles_path          |
| GET       | /api/articles/new      | api/articles#new     | new_api_article_path       |
| POST      | /api/articles          | api/articles#create  | api_articles_path          |
| GET       | /api/articles/:id      | api/articles#show    | api_article_path(:id)      |
| GET       | /api/articles/:id/edit | api/articles#edit    | edit_api_article_path(:id) |
| PATCH/PUT | /api/articles/:id      | api/articles#update  | api_article_path(:id)      |
| DELETE    | /api/articles/:id      | api/articles#destroy | api_article_path(:id)      |
+-----------+--------------------------+------------------------+------------------------------+

It is important to note that after doing this your controller will have to be located on controllers/api/.

You can read more about it here

  • I appreciate the help, I’ll test it out

Browser other questions tagged

You are not signed in. Login or sign up in order to post.