Git Product home page Git Product logo

rails-realworld-example-app's Introduction

Rails Example App

Example Rails codebase that adheres to the RealWorld API spec.

This repo is functionality complete -- PRs and issues welcome!

Check out the rails-5.1 branch to see the updated code for Rails 5.1

Getting started

To get the Rails server running locally:

  • Clone this repo
  • bundle install to install all req'd dependencies
  • rake db:migrate to make all database migrations
  • rails s to start the local server

Code Overview

Dependencies

  • acts_as_follower - For implementing followers/following
  • acts_as_taggable - For implementing tagging functionality
  • Devise - For implementing authentication
  • Jbuilder - Default JSON rendering gem that ships with Rails, used for making reusable templates for JSON output.
  • JWT - For generating and validating JWTs for authentication

Folders

  • app/models - Contains the database models for the application where we can define methods, validations, queries, and relations to other models.
  • app/views - Contains templates for generating the JSON output for the API
  • app/controllers - Contains the controllers where requests are routed to their actions, where we find and manipulate our models and return them for the views to render.
  • config - Contains configuration files for our Rails application and for our database, along with an initializers folder for scripts that get run on boot.
  • db - Contains the migrations needed to create our database schema.

Configuration

camelCase Payloads

null_session

By default Ruby on Rails will throw an exception when a request doesn't contain a valid CSRF token. Since we're using JWT's to authenticate users instead of sessions, we can tell Rails to use an empty session instead of throwing an exception for requests by specifying :null_session in app/controllers/application_controller.rb.

Authentication

Requests are authenticated using the Authorization header with a valid JWT. The application_controller.rb#authenticate_user! filter is used like the one provided by Devise, it will respond with a 401 status code if the request requires authentication that hasn't been provided. The application_controller.rb#authenticate_user filter is called on every request to try and authenticate the Authorization header. It will only interrupt the request if a JWT is present and invalid. The user's id is then parsed from the JWT and stored in an instance variable called @current_user_id. @current_user_id can be used in any controller when we only need the user's id to save a trip to the database. Otherwise, we can call current_user to fetch the authenticated user from the database.

Devise only requires an email and password upon registration. To allow additional parameters on sign up, we use application_controller#configure_permitted_parameters to allow additional parameters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.