Frontend Repo Link: Hello Rails Frontend
- Ruby on Rails API
- PostgreSQL
- Gems
- devise
- devise-jwt
- cancancan
- Gems
git@github.com:zainsadaqat/hello-rails-back-end.git
git clone git@github.com:zainsadaqat/hello-rails-back-end.git
cd hello-rails-back-end
bundle install
rails s
👤 Zain Sadaqat
- GitHub: @zainsadaqat
- Twitter: @zain_sadaqat
- LinkedIn: zain-sadaqat
- Create a new Rails API
rails new hello-world-back-end --api --database=postgresql
- Enable CORS(Cross Origin Resource Sharing)
- Go to config -> initializers -> cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
- Go to GemFile and un-comment
gem 'rack-cors'
- Create model, controller, database migration table and route via rails g resource command
rails g resource Greeting
- Specify attributes and datatypes of a secret menu item
class Greeting < ActiveRecord::Migration[6.0]
def change
create_table :greetings do |t|
t.text :message
end
end
end
rails db:migrate
- Define index, show, create, update, and destroy actions
module Api
module V1
class GreetingsController < ApplicationController
def index
@greetings = Greeting.all
render json: @greetings
end
def show
@greeting = Greeting.find(params[:id])
render json: @greeting
end
end
end
end
- Create routes for index, show, create, update, and destroy actions
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :greetings, only: [:index, :show, :create, :update, :destroy]
end
end
end
- Seed data
greeting1 = Greeting.create(message: "Hello from the other side 1")
greeting2 = Greeting.create(message: "Hello from the other side 2")
greeting3 = Greeting.create(message: "Hello from the other side 3")
greeting4 = Greeting.create(message: "Hello from the other side 4")
greeting5 = Greeting.create(message: "Hello from the other side 5")
- Fire up your server & postman to test API functionality
Credit:- Lisa's Article on dev.to
Generate Model
rails generate User name username email
Generate Resource
rails generate resource Greeting message:text
Generate Scaffold
rails generate scaffold User name username email
Rubocop
rubocop -A
Stylelinter
npx stylelint "**/*.{css,scss}"
Contributions, issues, and feature requests are welcome!
Feel free to check the issues page.
Give a ⭐️ if you like this project!
- Original design idea by Gregoire Vella on Behance.
This project is MIT licensed.