Example Rails application using Currentuser.io.
This application uses MySQL for demo purpose because it is widely used, but Currentuser.io works great with PostgreSQL too.
Note: this part of the documentation explains how the source code of this application has been built for educational purpose. You don't need to follow these steps if you only want to run the application.
1. The base of the application has been generated with Rails:
rails new currentuser-example-blog-rails --database=mysql
2. The file Gemfile has been enhanced with the following gems:
- activeuuid allows optimized uuid with MySQL (not required if we had used PostgreSQL)
- twitter-bootstrap-rails brings Twitter Bootstrap
- rails_12factor allows easier deployment on Heroku
- figaro allows easier local deployment
3. currentuser-services gem has been installed and configured, as explained in the documentation:
# Gemfile
gem 'currentuser-services'
# config/routes.rb
MyApplication::Application.routes.draw do
currentuser
end
# config/initializers/currentuser.rb
Currentuser::Services.configure do |config|
config.project_id = ENV['CURRENTUSER_PROJECT_ID']
end
4. The file config/database.yml has been adapted to make database configuration more flexible.
5. The base of the behavior has been generated with a Rails scaffold:
rails g scaffold Post user_id:uuid:index body:text
6. Twitter Bootstrap has been integrated:
rails g bootstrap:install static --no-coffeescript
rails g bootstrap:layout
rails g bootstrap:themed Posts
7. The PostsController has been protected, as explained in the documentation:
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_action :require_currentuser
8. A root route has been added:
# config/routes.rb
Rails.application.routes.draw do
root 'posts#index'
9. In app/controllers/posts_controller.rb, each created post is linked to current user:
# app/controllers/posts_controller.rb
def create
@post = Post.new(post_params)
@post.user_id = currentuser_id
10. Adapt app/views/posts/index.html.erb and app/views/posts/show.html.erb views to make the application look like a blog.
To retrieve a local version of this application:
git clone https://github.com/currentuser/currentuser-example-blog-rails.git
1. Create an application on heroku.com.
2. Configure your git repository (replace xxx
by the name of your application) and deploy it:
heroku git:remote -a xxx
git push heroku
3. Create an application on Currentuser.io (use https://xxx.herokuapp.com
as application URL)
and retrieve Project ID from the Currentuser.io settings.
4. Configure your application (replace ffffffff-ffff-ffff-ffff-ffffffffffff
with your Currentuser.io Project ID)
heroku config:set CURRENTUSER_PROJECT_ID=ffffffff-ffff-ffff-ffff-ffffffffffff
5. Provision and configure ClearDB plugin: https://devcenter.heroku.com/articles/cleardb
6. Initialize your database:
heroku run rake db:schema:load
See your platform documentation.
1. Install the gems
bundle
2. Create a MySQL user (you could skip this part if you prefer to use your root
user):
CREATE USER cu_ex_blog_rails@localhost IDENTIFIED BY 'cu_ex_blog_rails';
GRANT ALL ON cu_ex_blog_rails.* TO cu_ex_blog_rails@localhost;
3. Create an application on Currentuser.io (use http://localhost:3000
as application URL)
and retrieve Project ID from the Currentuser.io settings.
4. Create a configuration file:
# config/application.yml
DATABASE_URL: mysql2://cu_ex_blog_rails:cu_ex_blog_rails@localhost/cu_ex_blog_rails
# Replace 'ffffffff-ffff-ffff-ffff-ffffffffffff' with your Currentuser.io Project ID
CURRENTUSER_PROJECT_ID: ffffffff-ffff-ffff-ffff-ffffffffffff
5. Initialize your database:
rake db:setup