DEV Community

Building and Consuming a JSON API with Rails and React

Kattya Cuevas on October 10, 2019

When I thought about how to build an API, I started to search for what is the "best way" to do it. I found that exists specifications to build an A...
Collapse
 
kacalamaka profile image
Kacalamaka

Hi, I have an error when I run the command "foreman start -f Procfile.dev"

The error message are written below:

D:/CVWO/project/config/routes.rb:5:in block (2 levels) in <top (required)>': undefined methodjsonapi_resources' for #ActionDispatch::Routing::Mapper:0x... (NoMethodError)

Can somebody help me resolve this isssues?
I try look up for solutions on the internet, but there weren't any. I am also green to react-rails.

Collapse
 
athuyaoo profile image
Thuya

Did you install the gem jsonapi_resources?

Collapse
 
mikenath223 profile image
Michgolden Ukeje

Hi, Really love your post. But please when you wrote this:

This script will load the pack posts.jsx. We have to create that pack in the app/javascript/packs folder:

Where do i fix this code below:

import React from "react";
import ReactDOM from "react-dom";
import App from "components/App";

document.addEventListener("DOMContentLoaded", () => {
ReactDOM.render(
,
document.body.appendChild(document.createElement("div"))
);
});

Collapse
 
nicktohzyu profile image
nicktohzyu • Edited

You wrote

We're going to create the ApiController, to extend the controller from the ActionController::API module of Rails, and also we're going to include the JSONAPI::ActsAsResourceController from JSONAPI::Resources.

class ApiController < ActionController::API
include JSONAPI::ActsAsResourceController
end

Which file does this go in, and what is the path to that file? (am total beginner)

Collapse
 
athuyaoo profile image
Thuya

This should be in the controllers folder.

Collapse
 
thiagocardoso1988 profile image
Thiago Cardoso

any light on how to fix this error on Rails 6.1:

uninitialized constant ActionController::ForceSSL (NameError)

this happens when I try to spin up the server, after I installed jsonapi

Collapse
 
thiagocardoso1988 profile image
Thiago Cardoso

I saw an issue here, but without resolution :( github.com/cerebris/jsonapi-resour...

Collapse
 
kattyacuevas profile image
Kattya Cuevas

Hi Thiago! I've just seen the issue. There are other libraries to implement JSON API spec with rails. You can see them here: jsonapi.org/implementations/#serve...

Collapse
 
johnfajardo profile image
John Fajardo • Edited

Why use a gem for json instead of using the --api flag when creating the new rails project?

Collapse
 
kattyacuevas profile image
Kattya Cuevas

It's because I am building a web application which included an API inside. With the --api flag, I lose the initial configuration to render HTML views.

I did that instead of having 2 separated applications because I prefer to handle the routes with Rails, and after that, load the correct React application.

Collapse
 
lavaldi profile image
Claudia Valdivieso

I'll finally learn JSON API πŸ™Œ Thanks Kattya!