Back-end Engineering Articles
I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, and more...
2020-03-13
# Gemfile gem 'webpacker' # console $ rails webpacker:install # inside /config/webpack add staging environment file if its neccesary. Just copy de # same as production but change for staging names
- rails API under /api/v1/route - This API is protected by Devise - This API don't have inheritance from ::API base controller, but with ::Base - The authentication is not manage via Devise Auth Tokens, or any tokens at all, is managed by normal cookies as devise works - The test of the API can't be manage trough Postman, because we don't have any tokens. It need to be dealt under csfr-token that all Rails apps has, just like the tutorial does, but I have to allow Cross Origin - the react features are manage trought webpacker gem - the react front-end will be under /javascript folder - the react code need to call Turbolinks
$ rails new crud-rails-react $ rails g scaffold Beer brand:string style:string country:string quantity:integer $ rails db:migrate ## Add to Gemfile gem 'webpacker' ## console $ bundle install $ bundle exec rake webpacker:install $ bundle exec rake webpacker:install:react **## EVERYTHING NOW NEEDS TO BE DONE VIA YARN NOT NPM** ## add react and others yarn add react yarn add antd react-router-dom
# danielmorales is a full-stack rails app, so I don't have API Base, # instead the normal Base controller $ rails g scaffold todo task:text ## then - delete todo stylesheets - delete todo js - delete todo helpers - delete todo views - create api/v1 controller folder - create route namespace ## constroller class Api::V1::TodosController < ApplicationController ## routes namespace :api do namespace :v1 do resources :todos end end
class Api::V1::TodosController < ApplicationController before_action :set_todo, only: %i[show update destroy] # GET /todos def index @todos = Todo.all render json: @todos end # GET /todos/1 def show render json: @todo end # POST /todos def create @todo = Todo.new(todo_params) if @todo.save render json: @todo, status: :created, location: @todo else render json: @todo.errors, status: :unprocessable_entity end end # PATCH/PUT /todos/1 def update if @todo.update(todo_params) render json: @todo else render json: @todo.errors, status: :unprocessable_entity end end # DELETE /todos/1 def destroy @todo.destroy end private # Use callbacks to share common setup or constraints between actions. def set_todo @todo = Todo.find(params[:id]) end # Only allow a trusted parameter "white list" through. def todo_params params.require(:todo).permit(:task) end end
$ rails s -p 3001
$ npx create-react-app todo-frontend $ npm start