top of page
Search

Adding 'About' Section to the Group Resource - Back End

  • Writer: Chuck Curtis
    Chuck Curtis
  • Aug 19, 2019
  • 3 min read

Today I want to add a field for the description of a Group. As far at the UI is concerned, this will be a text-box for the user to enter any additional information about the group that they feel the other members should know like specific meeting location, what to bring, what to expect, etc. Before I update the UI, I want to add the new data field to the backend.


I'll start by updating the Group resource in the Ruby on Rails backend. I navigate into the backend directory in my terminal and create a new branch. Since I'm using Rails, I'll need to create an 'about' column in the groups table. In the terminal, I run the following command:

bin/rails g migration AddAboutToGroups about:string
The migration that will add the 'about' column to the 'Groups' table.

This command lets Rails know that I want to generate a migration which will add the column called 'about' to the groups table, and that the datatype for the new column will be a string. I open the db folder, and then open the latest file in the 'migrate' folder and confirm that the migration has the information that I'm looking for.


Next I run bin/rails db:migrate so that the migration will be applied. I open up the schema folder and see that the 'about' column has been added to the groups table, and everything is looking as it should.

The schema for 'groups', updated with the 'about' data.

Next I want to test the new 'about' data to make sure that I can add and update the 'about' information, so I open up the curl script I have for creating a new group and edit the script so that it will include data for 'about'. I go ahead and create a new group, this time adding some information in the 'about' section.

Updated CURL script, note the 'about' data has been added as the last entry.

After running the script, I take a look at the response and see that the 'about' info is null, which leads me to believe that the about info never made it to the database. I run the database in the terminal and use the SQL command SELECT * FROM groups and I see that, sure enough, the 'about' column is empty for the group that I just added, even though I updated the curl script and included 'about' data.

In the first image above, you can see that after running the curl script, I get the 201 success response, however in the last line the 'about' data is null. In the second image, I'm in the database in the terminal and I confirm that the 'about' data in the row with id 18, the entry that I just created using the curl script, is empty. This shows me that the data never made it to the database.


Since I've run into this before I know that I need to open up the group controller in the Rails server and add the 'about' info the list of accepted parameters. I go ahead and add 'about' to the list of params, shown in the first image below. I also remember that the only way that the server will send back the about info in the response is if I add 'about' to the serializer as well, shown in the second image below. I go ahead and add the 'about' info and sure enough, I run the curl script again and get the response I'm hoping for.

Now that I know that the 'about' data is working when I create a new group, I want to test out the new 'about' column when I'm updating a group. I make a similar change to the curl script for updating a single group and give it a try. Everything is working how I expect, so I commit the changes.

 
 
 

Comments


Post: Blog2_Post

Subscribe Form

Thanks for submitting!

  • Facebook
  • Twitter
  • LinkedIn

©2019 by Web Dev with Chuck. Proudly created with Wix.com

bottom of page