Build a Javascript Single Page Application

  1. Once created, the set up is fairly easy. You create a new rails app using the command: ‘rails new . — api’ (the dot is a way to create it inside this new folder and not adding another folder)
  2. Set up your models. I decided to go with a restaurant model that has a name and city, that belongs to a style that has a name. I decided to start very easy at first as I was not very confident, and later added an image and a link_url to my restaurant. To set up both of these models, I used scaffolding which set up 90 % of your controllers (CRUD actions), tables, routes and models.
  • Model: Things I had to do was to add the associations in my models, create a custom association writer method for the restaurant to accept the style attributes.
  • Controller: Add the style_attributes custom method in the strong params and in index and create actions, remove “created_at”, “updated_at” and “style_id” from render and include the “style” using:
except: [:created_at, :updated_at, :style_id], include: [:style]
  • Don’t forget to add rack-cors gem to your gemfile just by uncommenting it (The CORS spec allows web applications to make cross domain AJAX calls without using workarounds such as JSONP) and also update the cors file in your initializer to include all (*)
  1. Inside of this new folder, I created folders and files like so:
const main = () => document.getElementById("main")

--

--

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
5beberson

5beberson

French guy who recently moved to the United States. I am also changing career after having worked 13 years in restaurants. I will be going into coding.