In separation of the front and back ends of Rails + React, how to find API endpoints and React routes?

0

Issue

I took over a separate front-end and back-end website, consisting of two back-ends, Ruby on Rails and React. Prior to this, I have always used the MVC framework to make websites, but now the "V" part has become a React repo independently.

Specifically, I have to write a render json: @variable in a Controller#Action, but after this I don’t know where the json is transmitted to the front end (React repo)? I don’t know how to find the API endpoint.

I heard that this is related to React route? What’s this? Where is this written?

The problem is very big, please let me know if you have any recommended teaching articles.

Solution

If you are making Rails as backend and React as frontend you need to call Rails API from React. You need to return json from Rails method, you can declare format also in method so if format type is json return as json else return html.You can check confi/routes to know path for method.
To check if rails API call is working and how data is getting return you can use postman. Once tested in postman same api with http method (get,put, post etc) and parameter can be passed as API in React.

example

Blockquote

loadTdlists() {
axios
  .get("users/user_list.json")
  .then((res) => {
    this.setState({ tdlists: res.data });
  })
  .catch((error) => console.log(error));

}

Blockquote

here user_list method is define in users controller. you can use full URL also when calling api according to project requirement.

Answered By – GAURAV SETH

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More