Create an Inquiry Form with the Ruby SDK
Overview
There are many cases where inquiries and contact forms are configured and managed on a company's website. This article introduces how to manage these inquiries using the Kintone REST API client for Ruby and an inquiry form on a website created with Ruby on Rails. Use the kintone_rb SDK by SonicGarden for this tutorial.
Note
The SDK introduced in this article is a 3rd party SDK. Check the licenses, functions, etc. before using.
This tutorial uses the following tools:
- Rails Development Environment (For those new to Rails, refer to Getting Started with Rails to set up an environment)
- Ruby 2.7.0 or later (This article uses Ruby 2.7.5, Rails 7.0.2.)
- A Kintone account with the Help Desk Management App
Create the App
Add the Help Desk Management App
Log into Kintone and add the Help Desk Management App from the Kintone Marketplace. Refer to Adding a Sample App from the Kintone Help for more details.
Navigate to the Help Desk Management App and click the Change app settings icon (gear icon) to access the App settings.
Add a Text field to the form for the Company name. Then update the field title and field code for each field in the form to match the table below. Save the form when finished.
Field Type | Field Name | Field Code |
---|---|---|
Text | Company | Company |
Text | Name | Customer_name |
Radio Button | Inquiry Type | QType |
Text Area | Details | Detail |
Generate an API Token
Next, open the App Settings tab and click API Token under Customization and Integration.
Click the Generate button and check Add records. Then save the settings.
Make a note of the generated API token, as it will be necessary later for the customization code. Also make a note the App ID (in the following sample application, the App ID is 272).
Finally, to apply the App settings, click the Update App button.
Settings for the Help Desk Management App are now complete.
Create an Inquiry Form with Ruby
Step 1
Next, make an inquiry form application with Ruby on Rails.
(For those without a development environment for Ruby on Rails, refer to
Getting Started with Rails
to set up an environment.)
Open the terminal and execute the following command.
|
|
This creates a Rails application called contact.
As the subsequent commands are executed in the created contact directory, move to the directory with the following command.
|
|
Step 2
Before creating the form, install the kintone_rb SDK .
Open Gemfile and add the following command.
|
|
After saving the file, execute the following command.
|
|
Step 3
Create a controller with the following command. As an example, the controller is named inquiries.
|
|
Step 4
Open the routes.rb file in the config folder.
Edit it to match the following code.
|
|
Step 5
Create a new.html.erb file in the inquiries folder under the views folder.
Using the following code in the newly created new.html.erb file, create an inquiry form by using FormBuilder.
|
|
The following form should be generated.
Step 6
Generate a model of the resource name specified earlier.
Run the following command to generate the inquiry model.
|
|
This generates the following model.
Field Name | Field Type |
---|---|
company_name | string |
client_name | string |
contact_type | string |
details | text |
Execute migration with the following command to create a table in the database.
|
|
An Inquiries table is now created in the database.
In this case, the default database sqlite 3 was used. If sqlite 3 has not been installed yet, execute the following command to install it.
|
|
Step 7
Edit the controller created earlier.
Open inquiries_controller.rb in the controllers folder with an editor.
Type the following code into the inquiries_controller.rb file. Replace the {subdomain}, {API Token}, and {App ID} in lines 17 and 18 with the name of the Kintone subdomain, the API token of the App, and the App ID.
|
|
Code Description
Step 4
The routing within the above code is defined as follows to create the inquiry form.
HTTP method | Path | Field Code | Purpose |
---|---|---|---|
GET | /inquiries | inquiries#new | Returns an HTML form to create one inquiry |
POST | /inquiries | inquiries#create | Creates one inquiry |
The resource name specifies the inquiries created earlier.
|
|
The code also sets a form for creating an inquiry on the homepage.
|
|
For the sake of simplicity, only the new and create actions of the controller are defined.
The inquiry form is displayed with the new method (as there is no processing in particular, the method is blank) and the input data is saved with the create method.
|
|
After saving the form data in the database, the data is registered into the Kintone App.
|
|
Step 7
The following code loads the kintone_rb and creates an instance.
Specify the sub-domain name, the API token, and the App ID noted in the
Generate an API Token
section (refer to the
README
for more details).
|
|
The record data is set using Hashes, and is registered into the Kintone App via REST API.
|
|
Test the Integration
Start the local Rails server with the following command.
|
|
Accessing http://localhost:3000
through the browser will display the following inquiry form.
Enter in data and click the Send button to add a new record to Kintone.