Ready for lots of CRUD
Tags: ruby, rails
Published: 2019-08-31 23:41:17

<h1><center>Rails + React + Redux - Pt 2 </center></h1>
*********************************************************

This post is going to focus on setting up the initial Rails M&C aspects of the project. The V aspects will be handled by React in later posts. The following post will highlight some of the scraping used for starter data. 

*********************************************************
<h1><center> Let's get started! </center></h1>
*********************************************************

*I used the scaffold generator to get my models, controllers, serializers, and routes initiated. The --api tag ensures that the scaffold generator does not create the typical Rails view files, among others. Scaffold still generates some code that isn't needed, but I found -for me- it was helpful to start with this boilerplate and scale back as the project wrapped up.*


```rails g scaffold queen
rails g scaffold season
rails g scaffold episode
rails g scaffold appearance
rails g scaffold trivia
rails g scaffold quotes```

*********************************************************

*A DRAGnet queen has many episodes through appearances. Appearances are used to join queens and episodes, in addition to storing the queens stats during that episode. A queen also has many trivia and quotes. I wanted access to episodes, appearances, quotes, and trivia through the relevant queens. Notice that Rails changed trivia (plural) to trivium (singular)- go-go Rails grammar.*

{% gist https://gist.github.com/JaredHarbison/2ea835683dedfc200bb9f78ccd35f923 %}{% gist https://gist.github.com/JaredHarbison/25f9d7456c59aa154db4f68e54ac92b2 %}
{% gist https://gist.github.com/JaredHarbison/e485fc2a4881bcd9295988fc9e685e02 %}
{% gist 
https://gist.github.com/JaredHarbison/49cb51776029b1773fb4a6cb776ba34f %}
{% gist https://gist.github.com/JaredHarbison/8d4d2d2137c1a55b9dd0a2664646e51c %}

*********************************************************

*Rails naturally pushed the controllers into the controllers directory. I needed to set this directory to account for adding '/api/v1' to my routes.rb file later on. I created an 'api' folder within the controllers folder and a 'v1' folder within 'api'. __I Updated the class in each file accordingly.__ At this point, I didn't change much in the controllers except for each params method. To save space in this post I'll only show quotes_controller.rb. I changed the params method in the other controllers to mirror quotes_params. My namespaced routes.rb file will be really simple, as I'll handle most routing through React later on.*

{% gist https://gist.github.com/JaredHarbison/5f8e0204f786d186974b430061a2e4f7 %}
{% gist https://gist.github.com/JaredHarbison/b233cc7959d922ddb0d5968a2f08b4cd %}
{% gist https://gist.github.com/JaredHarbison/27a82f620a34e324d3996b99e05d4269 %}

*********************************************************

*Scaffold thankfully generates serializer files, which I needed to update.*
{% gist https://gist.github.com/JaredHarbison/35816846d5e08afecf6fa16e4c8ad13a %}

{% gist https://gist.github.com/JaredHarbison/34420f14e9188f51f001175f6c18273c %}

{% gist https://gist.github.com/JaredHarbison/1d2e45e42f24afdb8780018b7e186446 %}

{% gist https://gist.github.com/JaredHarbison/3748d014bc8c67cbddcfc197e2ea7bcc %}

{% gist https://gist.github.com/JaredHarbison/0be9cac317da319a74e2bf51866d6244 %} 

*********************************************************

*Finally one of my favorite parts, the migration files! I declared all of the attributes each model initially needed for the project. Rather than viewing the gists on this page, please check out the [repository](https://github.com/JaredHarbison/DRAGnet/tree/master/db/migrate) for the project for each of the models I generated (but heads up- the repository is further along than this blog series).*

*********************************************************

*To be sure everything is ready...*

```rails db:drop && rails db:create && rails db:migrate```

*********************************************************

*The next post will be more exciting. I'll scrape queens and seasons data using CSS and xPath selectors. There will be some fun edge-cases to work through to ensure each queen has some data to view and manipulate later on*
<h1><center> That's all folks! </center></h1>