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.

rails-realworld-example-app's People

Contributors

andyw8 avatar apai4 avatar askl56 avatar ericsimons avatar hum4n01d avatar kevyder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-realworld-example-app's Issues

`json` v 1.8.3 won't install

I'm on MacOS Sierra (10.12.4) and ruby v 2.4.0.

json -v 1.8.3 won't install for me. Throws C++ compilation issues, due to certain C++ libraries that were missing.

I am getting CROS issue.

Origin is not allowed by Access-Control-Allow-Origin.
When I try to access the api from the react app.
My rails-realworld-example-app running on port 3000
and react-redux-realworld-example-app running on port 8000

Cannot Register

When trying to Register, I keep getting the following (when used with Vue.js frontend):

(No route matches [OPTIONS] "/api/users"):

Failures when testing against the Realworld API Spec

The following tests appear to be failing when running the Realworld API spec against the Rails app:

  #  failure                       detail

 1.  AssertionError                Response contains "user" property
                                   expected false to be truthy
                                   at assertion:0 in test-script
                                   inside "Profiles / Register Celeb"

 2.  AssertionError                User has "email" property
                                   expected false to be truthy
                                   at assertion:1 in test-script
                                   inside "Profiles / Register Celeb"

 3.  AssertionError                User has "username" property
                                   expected false to be truthy
                                   at assertion:2 in test-script
                                   inside "Profiles / Register Celeb"

 4.  AssertionError                User has "bio" property
                                   expected false to be truthy
                                   at assertion:3 in test-script
                                   inside "Profiles / Register Celeb"

 5.  AssertionError                User has "image" property
                                   expected false to be truthy
                                   at assertion:4 in test-script
                                   inside "Profiles / Register Celeb"

 6.  AssertionError                User has "token" property
                                   expected false to be truthy
                                   at assertion:5 in test-script
                                   inside "Profiles / Register Celeb"

 7.  AssertionError                Response code is 200 OK
                                   expected false to be truthy
                                   at assertion:0 in test-script
                                   inside "Profiles / Profile"

 8.  AssertionError                Response code is 200 OK
                                   expected false to be truthy
                                   at assertion:0 in test-script
                                   inside "Profiles / Follow Profile"

 9.  AssertionError                Response code is 200 OK
                                   expected false to be truthy
                                   at assertion:0 in test-script
                                   inside "Profiles / Unfollow Profile"

rake aborted! NoMethodError: undefined method 'new' for BigDecimal:Class

$ rake db:migrate --trace
rake aborted!
NoMethodError: undefined method new' for BigDecimal:Class C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-4.2.6/lib/active_support/core_ext/object/duplicable.rb:85:in class:BigDecimal'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-4.2.6/lib/active_support/core_ext/object/duplicable.rb:80:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-4.2.6/lib/active_support/core_ext/object.rb:3:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/activesupport-4.2.6/lib/active_support/core_ext/object.rb:3:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/configuration.rb:2:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/configuration.rb:2:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/railtie.rb:2:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/railtie.rb:2:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/engine.rb:1:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/engine.rb:1:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/application.rb:7:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/application.rb:7:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails.rb:11:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails.rb:11:in <top (required)>' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/all.rb:1:in require'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-4.2.6/lib/rails/all.rb:1:in <top (required)>' C:/Users/elnew/Documents/real-world-rails-example/config/application.rb:3:in require'
C:/Users/elnew/Documents/real-world-rails-example/config/application.rb:3:in <top (required)>' C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in require'
C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in require' C:/Users/elnew/Documents/real-world-rails-example/Rakefile:4:in <top (required)>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/rake_module.rb:29:in load' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/rake_module.rb:29:in load_rakefile'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:703:in raw_load_rakefile' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:104:in block in load_rakefile'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:186:in standard_exception_handling' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:103:in load_rakefile'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:82:in block in run' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:186:in standard_exception_handling'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/lib/rake/application.rb:80:in run' C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/rake-13.0.3/exe/rake:27:in <top (required)>'
C:/Ruby27-x64/bin/rake:23:in load' C:/Ruby27-x64/bin/rake:23:in

'

Enviroment:
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x64-mingw32]
rails 4.2.6

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.