Create Rich Web Apps with Ruby on Rails and App42 Backend

In this blog post, I am going to make you familiar with the process of integrating App42 BaaS with rails4. But before we proceed, if you haven’t used App42 BaaS in your rails application anytime before this, then please check out our previous blog post Using App42 Backend as a Service SDK in Ruby On Rails. Here are the steps to get started with the integration: To get going, first you need to create an application using below command: Rails New App 1024x500 Create Rich Web Apps with Ruby on Rails and App42 Backend In the above command, I used –skip-active-record to avoid to generate ORM files as App42 BaaS will take care of your app’s Backend.

Now switch to your application directory and include App42_RUBY_SDK gem in your application Gemfile.

1
gem 'App42_RUBY_SDK'

Once you are done with it, run bundle update command which will update all your gem dependencies to their latest versions. Bundle Update Create Rich Web Apps with Ruby on Rails and App42 Backend Now it’s time to create two configuration files, which will load your App42 API and Secret key into your application and will create an instance of ServiceAPI.

First Create a YML file app42baas.yml inside the config folder of the application, which looks like:

1
2
3
4
5
6
development:
    api_key: "your api key"
    secret_key: "your secret key"
production:
    api_key: "your api key"
    secret_key: "your secret key"

Then create a second file in config/initializers folder e.g. app42_load.rb and paste the following code inside that app42_load.rb

1
2
3
require 'App42_RUBY_SDK'
APP_CONFIG = YAML.load_file("#{Rails.root}/config/app42baas.yml")[Rails.env]
$api = App42::ServiceAPI.new(APP_CONFIG['api_key'], APP_CONFIG['secret_key'] )

Now you can use $api object inside your application to get the instance of the particular API that you wish to build.
Let’s use User Service API to create user and get all users. To get started, we need to create a user controller using the below command: Rails Controller 1024x357 Create Rich Web Apps with Ruby on Rails and App42 Backend It creates user controller and respective views. Replace below code with existing user controller code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class UserController < ApplicationController
@@user_service = $api.buildUserService
def new
end
	def index
	@user_list = @@user_service.get_all_users 
end 
	def create
    begin
    @@user_service.create_user(params['user_name'], params['password'], params['email'])
    redirect_to user_index_path, notice: 'Thank you!, New user createed successfully.'
    rescue Exception => e
    redirect_to user_index_path, notice: "#{e.message}"
    end
  end
end

At the same time, you need to modify your views to create user and show user details.

/views/users/new.html.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%= link_to "Add new User", user_new_path %>
</br></br></br>
<% unless @user_list.nil? %>
  <table  border="1">
  <tr>
    <td>Name</td>
    <td>Email</td>
  </tr>
  <% for user in @user_list do %>  
	<tr>
      <td><%= "#{user.userName}" %></td>
      <td><%= "#{user.email}" %></td>
    </tr>
  <% end %>  
<% end %>

You are almost done with the integration. For the final touch, change routes(config/routes.rb).

1
root 'user#index'.

Source Code can be downloaded or viewed from our git repo.If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com.

twitter Create Rich Web Apps with Ruby on Rails and App42 Backendfacebook Create Rich Web Apps with Ruby on Rails and App42 Backendgoogle Create Rich Web Apps with Ruby on Rails and App42 Backendlinkedin Create Rich Web Apps with Ruby on Rails and App42 Backendpinterest Create Rich Web Apps with Ruby on Rails and App42 Backendreddit Create Rich Web Apps with Ruby on Rails and App42 Backendstumbleupon Create Rich Web Apps with Ruby on Rails and App42 Backendtumblr Create Rich Web Apps with Ruby on Rails and App42 Backend

Leave a Reply

Your email address will not be published. Required fields are marked *


+ four = 9

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>