Hello Rails Backend

Frontend Repo Link: Hello Rails Frontend

Built With

  • Ruby on Rails API
  • PostgreSQL
    • Gems
      • devise
      • devise-jwt
      • cancancan

Getting Started

copy

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

Authors

👤 Zain Sadaqat

Steps to create an API in Rails

  1. Create a new Rails API
rails new hello-world-back-end --api --database=postgresql
  1. Enable CORS(Cross Origin Resource Sharing)
    1. 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
  1. Go to GemFile and un-comment
gem 'rack-cors'
  1. Create model, controller, database migration table and route via rails g resource command
rails g resource Greeting
  1. 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

  1. 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
  1. 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
  1. 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")
  1. Fire up your server & postman to test API functionality

Credit:- Lisa's Article on dev.to

Other useful commands

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

🤝 Contributing

Contributions, issues, and feature requests are welcome!

Feel free to check the issues page.

Show your support

Give a ⭐️ if you like this project!

Acknowledgments

  • Original design idea by Gregoire Vella on Behance.

📝 License

This project is MIT licensed.