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

0

Hello, I'm developing a common system with ROR and it has a structure not just an API, it has its views and everything.

The question is that I'll also build an API to serve it for a mobile app and when I go about it I'll just find people explaining how to build an app just like api through the parameter:

rails new [my-app] --api

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

How do I do this?

    
asked by anonymous 05.10.2018 / 13:23

2 answers

1

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

Then you will have an API layer and a Asset Pipeline layer, the Rails default. To create this API layer, create a folder in app/controllers/api and place 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 it inherits from ActionController::API , the same as would inherit if you created the application with the --api flag, the difference is that you keep 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 the Asset Pipeline running along with an API, escaped in API:: .

    
06.10.2018 / 14:47
1

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

namespace :api do
  resources :articles
end

That 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's important to note that after doing this your controller will have to be located in controllers/api/ .

You can read more about this here

    
05.10.2018 / 14:53