Quickstarters
Feature Overview
How to Build a Backend for Ruby?
34 min
introduction in this tutorial, you’ll discover how to build a backend for ruby using back4app we will walk through essential back4app features like database management, cloud code functions, rest and graphql apis, user authentication, and real time queries (live queries) by leveraging the ruby programming language and back4app, you gain a secure, scalable, and robust backend that easily integrates with your software development ecosystem this approach saves time and effort by using an open source web platform built on parse you will see how to accelerate development projects with rapid development principles along the way, you’ll learn to use advanced security features, schedule tasks with cloud jobs, and set up webhooks for external integrations by the end, you’ll be ready to craft a web application framework structure and expand it for production applications you’ll be able to integrate ruby with back4app for data models, object oriented logic, and real time data take advantage of the development process best practices for application development in a model view controller (mvc) style or any other architecture you prefer enhance your role as a backend developer by handling secure user authentication, cloud code triggers, and more further refine your web applications to meet real world needs using the flexible features of back4app prerequisites a back4app account and a new back4app project getting started with back4app https //www back4app com/docs/get started/new parse app sign up for free if you do not have an account a local ruby environment you should have ruby https //www ruby lang org/en/documentation/installation/ installed typically, ruby 2 6 or above is recommended familiarity with the ruby programming language basic knowledge of ruby syntax, object oriented concepts, and common programming languages usage bundler (optional but recommended) bundler helps manage gems for your web application framework or development projects bundler official docs https //bundler io/ make sure you have all these prerequisites in place before starting having your back4app project created and your ruby environment ready will provide a smooth tutorial experience step 1 – creating a new project on back4app and connecting why this step matters a new back4app project is the foundation for your application development it hosts your database, manages user authentication, and provides the environment for running cloud code create a new project log in to back4app click new app in your back4app dashboard give your app a name (e g , “ruby backend tutorial”) install parse sdk and connect to back4app the parse sdk for ruby helps you interact with back4app’s backend you can install it via gem step 2 – setting up the database 1\ creating a data model in back4app, a class is like a database table for instance, you can make a “todo” class for storing tasks you can create classes on the fly from ruby or define them in the dashboard for fine grained control 2\ creating a data model using the ai agent open the ai agent in your app dashboard describe your data model in plain language (e g , “a todo app with title, iscompleted fields”) let the ai agent create your schema automatically 3\ reading and writing data using ruby (parse sdk) below is a simple example of creating a new record in a “todo” class using the ruby sdk require relative 'parse config' \# create a todo object todo = parse object new("todo") todo\["title"] = "buy groceries" todo\["iscompleted"] = false saved todo = todo save puts "todo saved with objectid #{saved todo\['objectid']}" \# query todos query = parse query new("todo") results = query get puts "fetched #{results size} todos " results each do |t| puts "title #{t\['title']}, completed #{t\['iscompleted']}" end 4\ reading and writing data using rest api you can also create and retrieve records using the rest interface curl x post \\ h "x parse application id your application id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"title" "buy groceries", "iscompleted" false}' \\ https //parseapi back4app com/classes/todo 5\ reading and writing data using graphql api back4app offers a graphql endpoint for advanced web applications mutation { createtodo(input { fields { title "clean the house" iscompleted false } }) { todo { objectid title iscompleted } } } 6\ working with live queries (optional) enable live queries in the back4app dashboard (server settings) and subscribe from your ruby script if your environment supports websocket connections you can also use them for real time updates in a web application framework that supports concurrency step 3 – applying security with acls and clps 1\ overview acls (access control lists) and clps (class level permissions) protect your data in object oriented ways acls let you decide who can read or write each record clps let you manage permissions for an entire class 2\ setting class level permissions (clps) and acls go to the database section of your app and select your class, like “todo ” configure your class’s clps under “security” or “class level permissions ” to set acls on a record owner = parse user current todo = parse object new("todo") acl = parse acl new(owner) acl public read = false acl public write = false todo acl = acl todo\["title"] = "private task" todo save this ensures only the owner can read/write that particular todo step 4 – writing cloud code functions 1\ why cloud code cloud code helps embed business logic and validations right into the server it’s ideal for ensuring data integrity and performing tasks not exposed to clients 2\ example function in your project’s main js (or similar) on back4app, you can define a cloud function parse cloud define('calculatetextlength', (request) => { const { text } = request params; if (!text) { throw 'no text provided'; } return { length text length }; }); note although our main application uses ruby, cloud code in parse is javascript based this is how you embed server side logic on back4app 3\ deployment back4app cli back4app dashboard copy/paste your function into cloud code > functions and click deploy 4\ npm modules you can install npm modules (e g , axios) for your cloud code environment for example npm install axios in your main js , require it and make external api calls as needed step 5 – configuring authentication 1\ dashboard settings enable user authentication in your app’s app settings back4app uses parse user for user management 2\ sign up / log in with ruby require relative 'parse config' \# signing up a user user = parse user new({ username "alice", password "secret123", email "alice\@example com" }) begin user sign up puts "user signed up successfully" rescue => e puts "error #{e message}" end \# logging in a user logged in user = parse user login("alice", "secret123") puts "logged in as #{logged in user username}" 3\ social login to integrate social logins (facebook, google, apple), consult the social login docs https //www back4app com/docs/platform/sign in with apple implementation details vary for each provider step 6 – handling file storage 1\ setting up file storage back4app manages file uploads via the parse file object in ruby file = parse file new("image png", file read("/path/to/image png")) saved file = file save puts "file saved at #{saved file url}" \# attach file to an object photo = parse object new("photo") photo\["imagefile"] = saved file photo save puts "photo object created with file reference " 2\ example users can upload images, documents, or other files retrieve the url to display it in your frontend or other services 3\ security considerations configure your app to restrict file uploads to authenticated users if necessary step 7 – email verification and password reset 1\ why it matters email verification ensures valid email addresses for new accounts password reset allows your users to recover their accounts securely 2\ back4app dashboard configuration enable email verification set up custom templates for verification and password reset 3\ code example from ruby, you can request a password reset curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"email" "alice\@example com"}' \\ https //parseapi back4app com/requestpasswordreset this sends a password reset link to the user’s email step 8 – scheduling tasks with cloud jobs 1\ what cloud jobs do cloud jobs run periodic tasks like cleaning up old data or sending summary emails 2\ example job parse cloud job('cleanupoldtodos', async (request) => { const todo = parse object extend('todo'); const query = new parse query(todo); const now = new date(); const thirty days = 30 24 60 60 1000; const cutoff = new date(now thirty days); query lessthan('createdat', cutoff); const oldtodos = await query find({ usemasterkey true }); await parse object destroyall(oldtodos, { usemasterkey true }); return `deleted ${oldtodos length} old todos `; }); schedule it under app settings > server settings > background jobs in your back4app dashboard step 9 – integrating webhooks 1\ definition webhooks let you send http requests to external services whenever certain events occur in back4app this is great for linking to third party services or triggering tasks in other programming languages 2\ configuration go to your app’s back4app dashboard > more > webhooks , and add a new webhook with the url of your external service 3\ example send data to stripe or slack whenever a new “todo” is created alternatively, you can define triggers in cloud code and make http requests within those triggers step 10 – exploring the back4app admin panel 1\ where to find it in your app dashboard, click more > admin app , then enable it 2\ features graphical interface for data management without code tools for analyzing logs, scheduling background jobs, and more role based access control, allowing you to provide non technical users with a safe way to manage content conclusion you have built a secure and scalable backend for your ruby app on back4app, using the ruby programming language to connect with the parse api your development process now includes a database with advanced security features (acls, clps) real time data updates via live queries cloud code triggers for custom business logic user authentication and file handling scheduled tasks with cloud jobs and webhooks for external services a user friendly admin panel for data management with this foundation, you can extend your web applications or other programming languages projects to meet real world requirements whether you’re an experienced backend developer or just starting with ruby, back4app provides an object oriented and easy to use platform for rapid development feel free to integrate additional apis or features, and explore further customizations to align with model view controller (mvc) patterns or other architectural styles next steps refine your production environment by adding advanced caching, roles based access, or performance optimizations add more complex relationships among data classes to power real world use cases explore official back4app documentation for security, performance insights, and analytics experiment with other open source web frameworks to expand your application’s features enjoy building more robust and feature rich web applications with ruby and back4app!