DEV Community

Cover image for Ready for lots of CRUD
JaredHarbison
JaredHarbison

Posted on • Edited on

3 2

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.

class Queen < ApplicationRecord
has_many :trivia, dependent: :destroy
has_many :quotes, dependent: :destroy
has_many :appearances
has_many :seasons, through: :appearances
accepts_nested_attributes_for :trivia, :quotes, :seasons, :appearances
end
view raw queen.rb hosted with ❤ by GitHub
class Season < ApplicationRecord
has_many :appearances
has_many :queens, through: :appearances
end
view raw season.rb hosted with ❤ by GitHub

class Appearance < ApplicationRecord
belongs_to :queen
belongs_to :season
end
view raw appearance.rb hosted with ❤ by GitHub

class Quote < ApplicationRecord
belongs_to :queen
end
view raw quote.rb hosted with ❤ by GitHub

class Trivium < ApplicationRecord
belongs_to :queen
end
view raw trivium.rb hosted with ❤ by GitHub

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.

class Api::V1::QueensController < ApplicationController
before_action :set_queen, only: [:show, :update, :destroy]
#### Leave the generated CRUD in place ####
private
# Use callbacks to share common setup or constraints between actions.
def set_queen
@queen = Queen.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def queen_params
params.require(:queen).permit(:id, :drag_name, :real_name, :date_of_birth, :hometown, :current_city,
:instagram, :facebook, :youtube, :website, :imdb, :twitter, :wikipedia,
appearances_attributes: [:season_id],
seasons_attribues: [:season_name],
trivia_attributes: [:queen_id, :content],
quotes_attributes: [:queen_id, :content],
)
#params.fetch(:quote, {})
end
end

class Api::V1::QuotesController < ApplicationController
before_action :set_quote, only: [:show, :update, :destroy]
#### Leave the generated CRUD in place ####
private
# Use callbacks to share common setup or constraints between actions.
def set_quote
@quote = Quote.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def quote_params
params.require(:quote).permit(:id, :queen_id, :content)
#params.fetch(:quote, {})
end
end
view raw quotes.rb hosted with ❤ by GitHub

Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :appearances
resources :queens
resources :seasons
resources :trivia
resources :quotes
end
end
end
view raw routes.rb hosted with ❤ by GitHub

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

class AppearanceSerializer < ActiveModel::Serializer
attributes :id
belongs_to :queen
belongs_to :season
end
class QueenSerializer < ActiveModel::Serializer
attributes :id, :real_name, :drag_name, :primary_image,
:date_of_birth, :hometown, :current_city, :ethnicity,
:instagram, :twitter, :facebook, :youtube, :website, :imdb, :wikipedia
has_many :trivia
has_many :quotes
has_many :seasons, through: :appearances
end
class QuoteSerializer < ActiveModel::Serializer
attributes :id, :queen_id, :content
end
class SeasonSerializer < ActiveModel::Serializer
attributes :id, :season_name
has_many :queens, through: :appearances
end
class TriviumSerializer < ActiveModel::Serializer
attributes :id, :queen_id, :content
end

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)