DEV Community

Cover image for Ready for lots of CRUD

Posted on • Updated on

Ready for lots of CRUD

Rails + React + Redux - Pt 2

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.

Let's get started!

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.

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.

Scaffold thankfully generates serializer files, which I needed to update.

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

That's all folks!

Top comments (0)