Skip to content

Commit 1fe5f97

Browse files
committed
2 parents 2a58021 + f78570c commit 1fe5f97

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

README.md

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ and Rails 5 in a Virtual Box, React JS via create-react-app and connecting the f
99

1010
## Table of Contents
1111

12-
- Downloading Virtual Box
13-
- Downloading create-react-app
14-
- Creating a Rails API
15-
- Create a react frontend
16-
- Connect the API with React
12+
- [Downloading Virtual Box](#downloading-virtual-box)
13+
- [Downloading create-react-app](#downloading-create-react-app)
14+
- [Creating a Rails API](#rails-api)
15+
- [Create a react frontend](#create-a-react-frontend)
16+
- [Connect the API with React](#downloading-react-into-our-project)
1717

1818
## Downloading Virtual Box
1919

@@ -248,7 +248,7 @@ which allows us to call the json data from `localhost:3000/api/v1/movies`
248248

249249
6. Let's seed our sqlite database with some classic movies so we can practice getting data with GET requests to the API.
250250

251-
Copy and paste the following data to your `config/seeds.rb` file.
251+
Copy and paste the following data to your `db/seeds.rb` file.
252252

253253
```ruby
254254
Movie.create(name: "The Nightmare Before Christmas", rating: 5)
@@ -450,8 +450,81 @@ class Button extends Component {
450450
export default Button;
451451
```
452452

453+
7. Finally, let's open our ```App.js``` file in the /src directory to add our new Button Component to the App itself. Check below to see the example. Don't forget that we need to enclose the ```<Button />``` in two ```<div></div>``` tags!
454+
```javascript
455+
import React, { Component } from 'react';
456+
import logo from './logo.svg';
457+
import './App.css';
458+
459+
import Button from './components/Button';
460+
461+
class App extends Component {
462+
render() {
463+
return (
464+
<div>
465+
<Button />
466+
</div>
467+
);
468+
}
469+
}
470+
471+
export default App;
472+
```
473+
453474
This should be everything we need to setup the API. Simply click our test api call button and see the magic work!
454475

455-
<a href="http://tinypic.com?ref=ere5n5" target="_blank"><img src="http://i68.tinypic.com/ere5n5.png" height="300" width="310" border="0" alt="Image and video hosting by TinyPic"></a>
476+
Congratulations! Our Rails API and React Client is done!
477+
478+
479+
## Rails Serializers
480+
481+
What are Serializers? Well Rails API's returns JSON data in full, so serializers allows us to cherry pick the exact data we want in a much organized fashion. Instead of getting every column from the returned data, we can grab which ever we allow to pass through.
482+
483+
| Normal Model | Serializer Model |
484+
| ------------- |:-------------:|
485+
| id, name, rating, director, score, actors_id, created_at, updated_at| id, name, rating|
486+
487+
We are able to tell the Rails API what to fetch rather than the frontend; making it much more simple and faster to scaffold your next project.
488+
489+
1. Installation
490+
491+
Open your ```gemfile``` and add the serializer gem into your project. Don't forget to ```bundle install``` !
492+
```ruby
493+
# Serializer
494+
gem 'active_model_serializers'
495+
```
496+
497+
We want to create a clone of any current model we have so when we make requests in the backend, the request will read the serializer file <strong>first</strong>, then it will find the rails model/controller to finisht the request. We have a model called Movie so we'll duplicate that by running:
498+
499+
```
500+
rails g serializer movie
501+
```
502+
You can see that a new directory was made in the ```app/``` directory and we now have ```app/serializers/movie_serializer``` file in our project.
503+
504+
Let's open that file and see what we have:
505+
```ruby
506+
class MovieSerializer < ActiveModel::Serializer
507+
attributes :id
508+
end
509+
510+
```
511+
512+
We have our Movie Class inheriting from the serializer class on the first line, and the returned attribute on the second. So far the default returned attribute is just an ID. Let's test this now!
513+
514+
1a. Turn on your rails server and go to the url ``` localhost:3000/api/v1/movies ```
515+
516+
You should see that only the ```id``` attribute is being returned from the database.
517+
```json
518+
{
519+
id: 1
520+
},
521+
{
522+
id: 2
523+
},
524+
{
525+
id: 3
526+
}
527+
```
528+
529+
You can add any attribute to your liking to the serializer file for your next big project. But that's the end of the serializer section!
456530

457-
### Congratulations! Our Rails API and React Client is done!

TODO.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TO DO
2+
3+
1. JWT
4+
2. Rack::Attack

0 commit comments

Comments
 (0)