- The domain and model served by the Rails backend must include a resource with at least one has-many relationship. (ex: Users have many associated models of a post or photo)
- The backend and frontend must collaborate to demonstrate Client-Server Communication with at least 3 AJAX calls, covering at least 2 of Create, Read, Update, and Delete (CRUD) operations.
The frontend file structure will be separated as follows:
Sending fetch requests to CoinGecko’s remote API
In our coin.js, we will be sending fetch requests to CoinGecko’s remote API were the data received will be rendered into our market table and stored into a static variable as follows:
The backend structure will be generated with the following commands:
$ rails new crypto-trading-simulator-backend --api$ rails g scaffold user username buying_power:decimal wallet_value :decimal$ rails g scaffold trades coin_id user_id:integer sym qty:integer cost:decimal
This should generate our backend structure on API mode, generate our models and migration files, as well as our controllers with the code that will automatically render out our data in as JSON.
Whitelisting domains with Rack CORS
We will be utilizing Rack CORS to whitelist the domains that are allowed to connect to our server. This should be commented out in our Gemfile already, so all we need to do is comment that in and run
// Gemfilegem 'rack-cors'
Then, go to /config/initializers/cors.rb
Comment the following code in, and set our origins to * (all), since we will be running this through a local server for now.
This will accept all incoming requests from all domains. Usually you would add the domain of where you are hosting your web app from (ex: Heroku).
Serializing our data with Fast JSON API
We will be utilizing Fast JSON to serialize the data that gets rendered into our API. We will need to include this into our Gemfile and run
// Gemfilegem 'fast_jsonapi'
Then, we can use rails to generate our serializer with the following command:
$ rails g serializer User name buying_power:decimal wallet_value:decimal$ rails g Trade coin_id user_id:integer qty:integer cost:decimal
And add our associations and the attributes to be rendered out as follows.
For more information about the project, you can view my GitHub.
For a video demonstration about how the project works and what it looks like, click here.