How I connect my Rails backend to the JS frontend

Yingqi Chen
Jun 21 · 5 min read
Image for post
Image for post

Set up: frontend mirrors backend

class Movie{
constructor(movie){
this.id = movie.id
this.title = movie.title
this.description = movie.description
this.rt_score = movie.rt_score
this.image = movie.image
this.release_year = movie.release_year
this.director = new Director(movie.director)
this.characters = movie.characters.map(c=>new Character(c))
}
...
}
class Director{
constructor(director){
this.name = director.name
this.introduction = director.introduction
this.image = director.image
this.id = director.id
this.addDirectorTotheForm()
}
...
}
class Character{
constructor(character){
this.name = character.name
this.image = character.image
this.introduction = character.introduction
}
...
}
ActiveRecord::Schema.define(version: 2020_02_02_012434) do  enable_extension "plpgsql"  create_table "characters", force: :cascade do |t|
t.string "name"
t.string "introduction"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "movie_id"
t.string "image"
end
create_table "directors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.text "introduction"
t.string "image"
end
create_table "movies", force: :cascade do |t|
t.string "title"
t.string "description"
t.integer "director_id"
t.integer "rt_score"
t.string "image"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.integer "release_year"
end
end
class Movie{
constructor(movie){
...
this.director = new Director(movie.director)
this.characters = movie.characters.map(c=>new Character(c))
}
...
}

Create new movies

params = {
..., "characters_attributes"=>{"0"=>{"name"=>"juju", "image"=>"", "introduction"=>""}, "1"=>{"name"=>"howl", "image"=>"", "introduction"=>""
}
}
let characters_attributes = {};
let inputs = inputArray[0]
let inputsForNames = inputArray[1]
let inputsForImages = inputArray[2]

for (let i = 0; i< character_counter +1; i++){

let name,image,introduction
name = inputsForNames[i].value
image = inputsForImages[i].value
introduction = textareas[i+1].value

if (!name==""){
let object = {
name: name,
image: image,
introduction: introduction
}
characters_attributes[i] = object
}
}

let formData = {
title: inputs[0].value,
director_id: select.value,
description: textareas[0].value,
image: inputs[1].value,
rt_score: inputs[2].value,
release_year: inputs[3].value,
characters_attributes: characters_attributes
}

let configObj = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(formData)
};

Delete existed movies

static deleteMovie(){
let result = confirm("Do you want to delete this movie?")
if (result){
let configObj = {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
id: this.id
})
};

fetch(`${BACKEND_URL}/movies/${this.id}`, configObj)
.then(resp=>resp.text())
.then(resp=>alert(resp))
.then(()=>document.getElementById(this.title).remove())
.catch(error=>console.log(error))
}
}

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Yingqi Chen

Written by

Web Developer | Ruby | Rails | SQL | Sinatra | React | Redux | HTML&CSS | JavaScript| MERN LinkedIn:https://www.linkedin.com/in/yingqi-chen/

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Yingqi Chen

Written by

Web Developer | Ruby | Rails | SQL | Sinatra | React | Redux | HTML&CSS | JavaScript| MERN LinkedIn:https://www.linkedin.com/in/yingqi-chen/

The Startup

Medium's largest active publication, followed by +705K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store