Quickstarters
Feature Overview
How to Build a Backend for Ruby On Rails?
34 min
introduction in this tutorial, you’ll learn how to build a backend for ruby on rails using back4app we’ll explore the key steps to integrate your rails application with essential back4app features—like database interactions, cloud code functions, rest and graphql apis, user authentication, and real time queries (live queries)—all running on the server side by leveraging the ruby programming language, you’ll build a robust and scalable architecture that aligns with the view controller mvc pattern in rails, enabling developers to speed up web application development you’ll also discover how back4app drastically reduces time and effort by simplifying server and database management these automated features can save you from manually setting up a web server or dealing with complex infrastructures by the end, you’ll possess a flexible and secure structure, ready for production or further expansions—like additional integrations and advanced custom logic whether you want to serve web pages or power data driven web applications, rails and back4app offer a seamless synergy for creating modern solutions in popular programming languages prerequisites to complete this tutorial, you will need a back4app account and a new back4app project getting started with back4app https //www back4app com/docs/get started/new parse app if you do not have an account, you can create one for free follow the guide above to get your project ready a ruby on rails development environment ensure you have ruby installed (preferably version 2 7 or above) and the rails gem (rails 6 or above) install rails https //guides rubyonrails org/getting started html parse ruby client if you intend to use the parse sdk directly, you can install the parse ruby client https //github com/parse community/parse sdk ruby gem to handle data operations with back4app familiarity with rails mvc if you’re new to ruby on rails, review the official rails guides https //guides rubyonrails org/ or a beginner’s tutorial to understand rails’ view controller mvc architecture before starting make sure these prerequisites are in place before you begin having your back4app project set up, plus your rails application scaffold ready, ensures a smooth tutorial experience step 1 – creating a new project on back4app and connecting create a new project the first step in setting up a server side backend for your rails application is creating a new project on back4app if you haven't done so log in to your back4app account click the “new app” button in your back4app dashboard give your app a name (e g , “rails backend tutorial”) once the project is created, it will be visible in your back4app dashboard this new project forms the core of all backend configurations in this tutorial connect the parse sdk (optional parse ruby client) to streamline database interactions and real time queries, back4app uses the parse platform if you want to integrate your rails server with parse directly, you may install the parse ruby client gem otherwise, you can rely on standard rest or graphql endpoints retrieve your parse keys in your back4app dashboard, go to your app’s “app settings” or “security & keys” to find your application id and rest api key you’ll also get your parse server url (e g , https //parseapi back4app com ) add the parse ruby client gem to your gemfile gem 'parse ruby client' then run bundle install initialize parse in an initializer, such as config/initializers/parse rb \# config/initializers/parse rb require 'parse' parse init application id 'your application id', api key 'your rest api key', server url 'https //parseapi back4app com' at this point, your rails application is able to communicate with back4app for storing and retrieving data, orchestrating custom logic, and more this integrated approach simplifies how your ruby on rails app handles the backend step 2 – setting up the database creating a data model in many web applications , you define your data structure in rails using activerecord migrations with back4app, you also have the option to build your schema directly on the dashboard for example, if you have a todo model navigate to “database” in your back4app dashboard create a new class named “todo” and add columns like title (string) and iscompleted (boolean) back4app supports string , number , boolean , date , file , pointer , relation , array , geopoint , and polygon rails typically manages these within its model definitions, but you can also let parse create columns automatically on first save (if you use the parse sdk or rest/graphql apis) creating a data model using the ai agent back4app’s ai agent can automate schema creation open the ai agent from your dashboard describe your data model (e g , “please create a new todo app with a complete class schema ”) let the agent generate your schema reading and writing data using sdk if you choose to integrate the parse ruby client , you can store a record like this \# app/controllers/todos controller rb def create todo class = parse object new('todo') todo class\['title'] = params\[ title] todo class\['iscompleted'] = false begin todo class save render json { message 'todo saved successfully' } rescue => e render json { error e message }, status 500 end end def index query = parse query new('todo') results = query get render json results end reading and writing data using rest api alternatively, you can use rest calls from within your rails code (or any external client) for instance, to create a todo 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" "finish rails guide", "iscompleted" false}' \\ https //parseapi back4app com/classes/todo reading and writing data using graphql api back4app also offers graphql mutation { createtodo(input { fields { title "plan web pages" iscompleted false } }) { todo { objectid title iscompleted } } } working with live queries (optional) if you want real time updates in your rails application , you can subscribe to live queries from a front end or a websocket client enable live queries in your back4app server settings , then connect to the wss\ //your subdomain b4a io for an ongoing stream of changes this is helpful for dynamic web applications that need immediate data refreshes step 3 – applying security with acls and clps brief overview security is vital in web application development back4app offers access control lists (acls) and class level permissions (clps) to control data visibility at both the object and class levels this helps ensure only authenticated or authorized users can read/write sensitive data setting up class level permissions go to the database in your back4app dashboard select a class (e g , “todo”) open the clps tab to configure read/write access by roles, authenticated users, or the public configuring acls you can set an acl on an object so that only a specific user can read or modify it in rails with parse ruby client , it might look like todo = parse object new('todo') acl = parse acl new \# set read/write for a user acl set read access for(user id, true) acl set write access for(user id, true) todo acl = acl todo save step 4 – writing cloud code functions why cloud code cloud code runs server side javascript, enabling developers to add custom logic, triggers, and validations without managing your own web server you might want to handle extra server logic or do checks before saving data example function below is an example in javascript while your rails server handles the main app, you can still use cloud code to process data parse cloud define('calculatetextlength', async (request) => { const { text } = request params; if (!text) { throw new error('no text provided'); } return { length text length }; }); deployment deploy through the back4app cli or from the back4app dashboard cloud code is a convenient way to encapsulate logic that’s shared among all clients, regardless of which programming languages they use with node based modules (npm) support, you can integrate external packages seamlessly step 5 – configuring authentication enable user authentication back4app manages user sign ups, logins, and sessions with parse’s built in user class you can create a user with a rest call or through any official sdk 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 '{"username" "john", "password" "secretpass"}' \\ https //parseapi back4app com/users social login you can integrate facebook, google, or apple sign in flows each provider has a slightly different approach, but the main principle is to exchange tokens with parse check the official social login docs https //www back4app com/docs/platform/sign in with apple for details on how to integrate these into your rails application step 6 – handling file storage setting up file storage using parse for files is as simple as uploading them through the parse api if you’re employing the parse ruby client file data = file open('path/to/image jpg') parse file = parse file new('image jpg', file data) begin parse file save puts "file uploaded #{parse file url}" rescue => e puts "error uploading file #{e message}" end example you can then attach this file to an object photo obj = parse object new('photo') photo obj\['imagefile'] = parse file photo obj save step 7 – email verification and password reset enable email verification in your back4app dashboard under email settings set up password reset by customizing the email template and domain settings trigger email flows automatically after user registration or when a user requests a password reset this ensures improved security and helps confirm valid email ownership step 8 – scheduling tasks with cloud jobs cloud jobs if you want to automate tasks like cleaning data or sending daily summaries, use cloud jobs 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); try { const oldtodos = await query find({ usemasterkey true }); await parse object destroyall(oldtodos, { usemasterkey true }); return `deleted ${oldtodos length} old todos `; } catch (err) { throw new error('error during cleanup ' + err message); } }); you can schedule such jobs in the back4app dashboard under server settings > background jobs to run daily or at desired intervals step 9 – integrating webhooks webhooks let your back4app app send data to an external url whenever certain events happen you can forward new or updated object data to another system—like stripe or slack go to more > webhooks in your back4app dashboard add a new webhook pointing to your external url (e g , a route in your rails controller) specify the event that triggers the webhook if you prefer, you can also trigger a webhook from cloud code by making an http request in a beforesave or aftersave function step 10 – exploring the back4app admin panel the back4app admin app offers a user friendly way for non technical teams or clients to perform crud operations on your back4app data this interface eliminates the need for direct code changes or usage of the parse dashboard for basic tasks where to find it you can enable the admin app from your dashboard by going to more > admin app and clicking enable admin app features once enabled, you can create and manage data without writing queries monitor logs, schedule background jobs, and handle push notifications (if enabled) provide role based access to team members for safer editing conclusion congratulations! you’ve learned how to build a backend for ruby on rails using back4app in this tutorial, you have configured a rails project to interact with back4app via rest, graphql, or the parse ruby client set up secure database interactions using class schemas, acls, clps, and optional live queries integrated user authentication with email verification and password resets deployed cloud code for custom logic, triggers, and scheduled tasks managed file uploads , harnessed webhooks, and explored the admin panel by combining ruby on rails’ mvc architecture with back4app’s robust feature set, your rails application can scale seamlessly you’ll save considerable time in managing infrastructure, letting you concentrate on building web applications with refined user experiences and reliable server side logic next steps extend your rails app by adding more complex relationships, caching strategies, or advanced integrations incorporate third party apis (like payment gateways) and harness cloud code for deeper web application development explore the official back4app documentation for advanced security, performance tuning, logs analysis, and more experiment with real time features to create chat apps or collaborative platforms—ideal for data that changes frequently with these fundamentals in place, you can rapidly develop, iterate, and scale your rails server for modern web applications happy coding!