You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Create a react frontend](#create-a-react-frontend)
16
+
-[Connect the API with React](#downloading-react-into-our-project)
17
17
18
18
## Downloading Virtual Box
19
19
@@ -248,7 +248,7 @@ which allows us to call the json data from `localhost:3000/api/v1/movies`
248
248
249
249
6. Let's seed our sqlite database with some classic movies so we can practice getting data with GET requests to the API.
250
250
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.
252
252
253
253
```ruby
254
254
Movie.create(name:"The Nightmare Before Christmas", rating:5)
@@ -450,8 +450,81 @@ class Button extends Component {
450
450
export default Button;
451
451
```
452
452
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
+
453
474
This should be everything we need to setup the API. Simply click our test api call button and see the magic work!
454
475
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.
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 classon 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!
456
530
457
-
### Congratulations! Our Rails API and React Client is done!
0 commit comments