<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tuyen Ha</title>
    <description>The latest articles on Forem by Tuyen Ha (@janestack).</description>
    <link>https://forem.com/janestack</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F830705%2F473c48be-bbd9-452a-9e90-62402520f8fe.jpeg</url>
      <title>Forem: Tuyen Ha</title>
      <link>https://forem.com/janestack</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/janestack"/>
    <language>en</language>
    <item>
      <title>Getting Started with Action Mailer and GMAIL Configuration</title>
      <dc:creator>Tuyen Ha</dc:creator>
      <pubDate>Tue, 15 Aug 2023 03:51:02 +0000</pubDate>
      <link>https://forem.com/janestack/getting-started-with-action-mailer-3e99</link>
      <guid>https://forem.com/janestack/getting-started-with-action-mailer-3e99</guid>
      <description>&lt;h2&gt;
  
  
  What Is Action Mailer?
&lt;/h2&gt;

&lt;p&gt;Action mailer is a component of the Ruby on Rails framework that makes sending emails from your application a simple process. It provides a framework for creating, sending, and managing email messages.&lt;/p&gt;

&lt;p&gt;So let's get started!&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;first step&lt;/strong&gt; is to generate and set up the mailer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% rails generate mailer WelcomeMail
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate the following outputs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create app/mailers/welcome_mail_mailer.rb
invoke erb
create app/views/welcome_mail_mailer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the # app/mailers/application_mailer.rb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ApplicationMailer &amp;lt; ActionMailer::Base
   default from: "from@gmail.com"
   layout "mailer"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a class called "ApplicationMailer" that inherits from the "ActionMailer::Base" class. Inside the class, the first configuration is "default from: '&lt;a href="mailto:from@gmail.com"&gt;from@gmail.com&lt;/a&gt;", which sets the default sender address. The second configuration is "layout 'mailer'", which sets the default layout template for the emails.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Be sure to replace the &lt;a href="mailto:from@gmail.com"&gt;from@gmail.com&lt;/a&gt; with your chosen default email.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Inside the # app/mailers/welcome_email.rb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class WelcomeMailer &amp;lt; ApplicationMailer
   default from: 'from@gmail.com'

   def welcome_email
      @user = params[:user]
      mail(to: @user.email, subject: "Welcome to 
      Random Posts")
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code defines a class called "WelcomeMailer" that inherits from the "ApplicationMailer" class. Similar to the Controller we are already used to, we can create an email by adding a method to the mailer.&lt;/p&gt;

&lt;p&gt;The configuration "default from: '&lt;a href="mailto:from@gmail.com"&gt;from@gmail.com&lt;/a&gt;'" sets the default sender email address for all the emails sent from the mailer class.&lt;/p&gt;

&lt;p&gt;Then there is a method called "welcome_email" which is responsible for sending the physical welcome email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CREATING THE MAILER VIEWS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the # app/views/welcome_mailer/welcome_email.html.erb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
   &amp;lt;head&amp;gt;
      &amp;lt;meta content='text/html; chatset=UTF=8' http-equiv='Content-Type' /&amp;gt;
   &amp;lt;/head&amp;gt;
   &amp;lt;body&amp;gt;
      &amp;lt;h1&amp;gt;Welcome to Random Posts, &amp;lt;%= @user.name %&amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;You've signed up for Random Posts!&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Thanks for joining us!&amp;lt;/p&amp;gt;
   &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is a basic ruby HTML file that creates the body of the email in an HTML structure.&lt;/p&gt;

&lt;p&gt;Inside the # app/views/welcome_mailer/welcome_email.text.erb&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Welcome to Random Posts, &amp;lt;%= @user.name %&amp;gt;
=============================================

You've signed up for Random Posts.

thanks for joining us!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we have the body of the email in text format.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CALLING THE MAILER&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside # app/controller/users_controller.rb &lt;/p&gt;

&lt;p&gt;We are going to instruct the WelcomeMailer to deliver a welcome email to anyone who signs up for an account!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UsersController &amp;lt; ApplicationController
   def create
      @user = User.new(user_params)
      if @user.save
         WelcomeMailer.with(user: @user).welcome_email.deliver_now
         session[:user_id] = @user.id
         render json: @user, status: 201
      else
         render json: {errors: @user.errors.full_messages}, status: :unprocessable_entity
      end
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The line WelcomeMailer.with(user: @user).welcome_email.deliver_now sends a welcome email using a mailer class named "WelcomeMailer." It passes the "@user" object to the mailer's "with" method. This method prepares the email, then calls the "welcome_email" method to generate the email content. Which then will send out the email when a new user has been created!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For this specific tutorial, I will be using a GMAIL account as the default sender email, and with that we'll continue on with the GMAIL configuration.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GMAIL CONFIGURATION&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside # config/environments/development.rb,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'example.com',
user_name:            'from@gmail.com',
password:             '**App Password**',
authentication:       'plain',
enable_starttls_auto: true }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding the following codes into our file development.rb, we have successfully configured and set up the Action Mailer and Mailcatcher.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The code above is specifically for GMAIL Configuration. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Keep in mind that in order to use Action Mailer with Gmail, an &lt;strong&gt;App Password&lt;/strong&gt; is required. This prevents the violation of your other security such as the 2-step verification.&lt;/p&gt;

&lt;p&gt;You can get an App Password by signing into your Google Account =&amp;gt; security =&amp;gt; and under Signing in to Google. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Turn on your 2-Step Verification if it's not already in use.&lt;/li&gt;
&lt;li&gt;Navigate to App Passwords&lt;/li&gt;
&lt;li&gt;Select Other (Custom name) and give it a name.&lt;/li&gt;
&lt;li&gt;GENERATE&lt;/li&gt;
&lt;li&gt;Copy your App Password from the yellow box.&lt;/li&gt;
&lt;li&gt;Replace the password with your App Password.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Just like that, you've successfully sent a welcome message!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Nested Routing in Ruby</title>
      <dc:creator>Tuyen Ha</dc:creator>
      <pubDate>Thu, 13 Apr 2023 22:41:52 +0000</pubDate>
      <link>https://forem.com/janestack/nested-routing-in-ruby-hd5</link>
      <guid>https://forem.com/janestack/nested-routing-in-ruby-hd5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why do we use Nested Routes?&lt;/strong&gt;&lt;br&gt;
In short, we use nested routes to make our codes cleaner which as a result makes them much easier to read. More importantly, nested routes make associating relationships less complex - nesting allows us to stay Restful and automatically routes the URL to where more than one model can be involved. Just by looking at how models are routed, it's clear which model belongs to which model and which models have many of which models.&lt;/p&gt;

&lt;p&gt;As an example,&lt;br&gt;
we have a user model that has many posts. By nesting the post model in the user model, you can route it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/users/2/posts&lt;/code&gt; # all the posts for user with id:2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Nested Routes&lt;/strong&gt;&lt;br&gt;
To start we need to set up nested resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g resource model Blog title content:text
rails g resource model Comment reply blog:belongs_to
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;rails generate&lt;/code&gt; command, or &lt;code&gt;rails g&lt;/code&gt; is a shortcut template to generate boilerplate code. By running &lt;code&gt;rails g resource&lt;/code&gt; a migration file, model, controller, and serializer will automatically get generated for you without you having to do much of the work yourself. That line of code will also open up all of the routes in config/routes.rb for you as well.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Side note:&lt;/strong&gt; Making use of these generators can save you so much time when working on Rails projects! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The blog model will contain a title as a string and content in text form &lt;code&gt;content:text&lt;/code&gt;. When title without :string, terminal will automatically know that you want it as a string. &lt;/p&gt;

&lt;p&gt;the blog:belongs_to line will generate a blog_id, which is a foriegn_key to blogs. This foreign key strongly enforces legitimate data at the DB level.&lt;/p&gt;

&lt;p&gt;Next you create routes,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do

  resources: blog 
  resources: comments

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we connect the parent and the child,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Comment &amp;lt; ApplicationRecord
  belongs_to :blog
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is saying that the comments belongs_to a blog. This relationship will allow the blog to have any number of comments.&lt;/p&gt;

&lt;p&gt;Because the comment belongs to the blog, you must also specify in the blog model that a blog has many comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Blog &amp;lt; ApplicationRecord
  has_many :comments, dependent: :destroy
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep in mind that the parent might get destroyed by certain events in the app and therefore we want to ensure that when the parent is deleted, all the children should be destroyed along with their parent components. Why? Because if it doesn't get destroyed all with its parent the belongs_to reference in the child object will have undesired consequences.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;dependent: :destroy&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;this line will take care of that.&lt;/p&gt;

&lt;p&gt;Now, the routes.rb file needs to be updated in order for the comment to be listed as a nested child of the blog.&lt;br&gt;
This is done as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do
  resources: blogs do
     # nested resource for comments
     resources: comments, only: [:index, :show]
  end
  resources: :comments
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 'index' and 'show' routes are nested routes and below the nesting are the regular resourced routes for comments. This allows the index action to deal with both the un-nested and nested routes.&lt;br&gt;
&lt;code&gt;/blogs/2/comments&lt;/code&gt; or &lt;code&gt;/blogs/comments&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although the comments are nested under the blogs we are still dealing with the comments. &lt;/p&gt;

&lt;p&gt;If you type in &lt;code&gt;rails routes&lt;/code&gt; in the terminal, you'll get a log of all the possible routes based on your routes.rb file. You'll be able to know for sure which routes are available. &lt;/p&gt;

&lt;p&gt;Then you'll need to let the controller know about this change and tell it what to do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CommentController &amp;lt; ApplicationController
def index
   if params[:blog_id]
      @comments = Blog.find(params[:blog_id]).comments
   else
      @comments = Comment.all
   end
end

def show
   @comment = Comment.find(params[:id])
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am telling the Comment Controller to render all comments under a specific blog "blog_id" when "/blogs/:id/comments" is being fetched. &lt;/p&gt;

&lt;p&gt;The nested resources give us a way to document the parent/child relationship between our routes and our URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Nested routes are great to use but keep in mind that nested routes can quickly become difficult to work with once the routes go beyond two layers deep. You can most definitely go beyond two layers deep but it's considered a code smell and best to be avoided.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Sinatra and Active Records to Perform CURD Actions</title>
      <dc:creator>Tuyen Ha</dc:creator>
      <pubDate>Wed, 25 Jan 2023 02:37:33 +0000</pubDate>
      <link>https://forem.com/janestack/sinatra-and-active-records-to-perform-curd-actions-4408</link>
      <guid>https://forem.com/janestack/sinatra-and-active-records-to-perform-curd-actions-4408</guid>
      <description>&lt;h1&gt;
  
  
  Sinatra
&lt;/h1&gt;

&lt;p&gt;Sinatra is an open source software web application library and domain-specific language written in Ruby. It does not follow the typical model-view-controller pattern used in other frameworks so therefore Sinatra is considered a microframework rather than a full-stack web development framework such as Ruby on Rails. Simply put, Sinatra is fully dependent on the Rack web server interface and focuses on quickly creating web applications in Ruby with a little less effort. Therefore, making it the perfect small web framework to get you started in web application development with Ruby.&lt;/p&gt;

&lt;h1&gt;
  
  
  Active Record
&lt;/h1&gt;

&lt;p&gt;Active record is a Ruby gem and a description of an Object Relational Mapping system (ORM). It's basically a Ruby code that runs in between your database and your logic code making changes to your database.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why Use Active Record?
&lt;/h1&gt;

&lt;p&gt;Now let's talk about why Active Record is used. React is used as a frontend interface in applications but React cannot communicate directly with the database and therefore we need a way to connect our frontend with our backend. This is where Application Programming Interface (API) comes in. Both of the applications uses HTTP and JSON which means we can use this fact to our advantage. Active Record is used to help our React application and our database to understand one another, resulting in a successful collaboration.&lt;/p&gt;

&lt;h1&gt;
  
  
  Performing CURD with Sinatra
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;For recap, CURD stands for CREATE, UPDATE, READ, and DELETE. This is the four basic operations that provide users the ability to create data, access the data, update or edit the data and delete the data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first step is to create a migration:&lt;br&gt;
&lt;code&gt;$ bundle exec rake db:create_migration NAME=create_games&lt;/code&gt;&lt;br&gt;
Running this command will generate a new file in &lt;em&gt;db/migrations&lt;/em&gt; and Rake task will also add some starter codes for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateGames &amp;lt; ActiveRecord::Migration[6.1]
   def change
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're creating a class called &lt;code&gt;CreateGames&lt;/code&gt; that inherits from Active Record's &lt;code&gt;ActiveRecord::Migration&lt;/code&gt; module. Inside that class, we have a &lt;code&gt;change&lt;/code&gt; method which it commonly used for updating the database. The result of this migration is a table with the appropriate columns for &lt;code&gt;games&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class CreateGames &amp;lt; ActiveRecord::Migration[6.1]
   def change
      create_table :games do |t|
        t.string :name
        t.string :genre
        t.integer :price
      end
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, &lt;code&gt;games&lt;/code&gt; because singular and plural makes a huge difference. Table names are plural because we are creating a &lt;code&gt;games&lt;/code&gt; table that we are using with a &lt;code&gt;Game&lt;/code&gt; class. In contrary, class names are singular. &lt;/p&gt;

&lt;p&gt;Then, we write a block of codes that will create our columns. &lt;/p&gt;

&lt;p&gt;We also need the primary key but Active Record will automatically generate that id column for us!&lt;/p&gt;

&lt;p&gt;Next, we run our migration:&lt;br&gt;
&lt;code&gt;$ bundle exec rake db:migrate&lt;/code&gt;&lt;br&gt;
This command tells Active Records to create a new database file if there isn't one already and update it. &lt;/p&gt;

&lt;p&gt;The next step is to create a file &lt;em&gt;game.rb&lt;/em&gt; for the &lt;code&gt;Game&lt;/code&gt; class and extend the class with &lt;code&gt;ActiveRecord::Base&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Class Artist &amp;lt; ActiveRecord::Base
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for the ability to create, update, read, and delete data.&lt;/p&gt;

&lt;p&gt;Let's start with the &lt;strong&gt;GET Request&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get "/games" do
   games = Game.all
   games.to_json
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code snippet above, we first get all the games from the database and return a JSON response with an array of all the game data as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  {
     "id": 1,
     "title": "Pokemon",
     "genre": "Role-playing:,
     "price": 28
  },
  {
     "id": 2,
     "title": "Mario Kart",
     "genre": "Racing",
     "price": 34
  }
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;POST Request&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post "/games" do
   games = Game.create(
      title: params[:title],
      genre: params[:genre],
      price: params[:price]
   )
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the HTTP verb /games to handle this request then access the data in the body of the request to create new games. Lastly, send a response to the newly created game as JSON.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;param is a special kind of variable in the computer programming language that is used to pass information between functions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;PATCH Request&lt;/strong&gt;&lt;br&gt;
In my opinion, the PATCH request is the trickiest request of them all because, from the frontend, a PATCH request needs to be utilized in order to handle certain features that allow a user to update their review.&lt;/p&gt;

&lt;p&gt;To handle a PATCH request, a PATCH HTTP verb to /games/:id is used in order to find the game that might need updating. We then need to access the data in the body of the request and use that data to update the game in the database. Then again, send a response of the update to JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;patch "/games/:id" do
   games = Game.find(params[:id])
   games.update(
      title: params[:title],
      genre: params[:genre],
      price: params[:price]
   )
   games.to_json
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;DELETE Request&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete "/games/:id" do
   games = Game.find(params[:id])
   games.destroy
   games.to_json
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one is fairly simple. All we need to do is find the game using the ID, delete that game using &lt;code&gt;games.destroy&lt;/code&gt;, and send a response with the deleted game to JSON.&lt;/p&gt;

&lt;p&gt;I hope this little tutorial will be proven helpful for those who need a little more clarification!&lt;/p&gt;

</description>
      <category>activerecord</category>
      <category>curd</category>
      <category>sinatra</category>
      <category>ruby</category>
    </item>
    <item>
      <title>React Controlled Components</title>
      <dc:creator>Tuyen Ha</dc:creator>
      <pubDate>Thu, 27 Oct 2022 21:10:29 +0000</pubDate>
      <link>https://forem.com/janestack/react-controlled-components-21f1</link>
      <guid>https://forem.com/janestack/react-controlled-components-21f1</guid>
      <description>&lt;p&gt;First, let's quickly go over what an uncontrolled component is and why it is advised to more often use controlled components over uncontrolled components. &lt;/p&gt;

&lt;p&gt;Uncontrolled components are components that are NOT controlled by the React State. They are taken care of by the DOM (Document Object Model).&lt;/p&gt;

&lt;p&gt;With the form data being handled by the DOM, when something is being typed into the input field, the data will be remembered by the DOM. This is considered "uncontrolled" because it relies on the DOM to manage the input data. Then when we want to have access to those data, a ref must be used, useRef(). Ref allows us to reference a DOM element or class component from within a parent component when the form is submitted but it doesn't allow us to validate the inputs as we type.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If validation is not necessarily needed, then using uncontrolled components might be the easiest way to create forms. Though, it might not be the most preferable way for the sake of our users.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, uncontrolled component does limit our control over the form elements and data.&lt;/p&gt;

&lt;p&gt;This is where using the controlled component is proven to be more beneficial! The controlled component is handled by the component itself rather than the DOM, which allows us better control over the form elements and data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When using controlled components, there is however one drawback. The number of states in a component increases with each controlled elements that are being added to the form element sometimes making it slightly harder to keep track of everything on the form.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What is controlled components:&lt;/strong&gt;&lt;br&gt;
A controlled component is referred to components that ARE controlled by using React state, useState(). In a controlled component, the parent components manages their own states and passes the new values as props. &lt;/p&gt;

&lt;p&gt;For example, when populating forms the data are being handled by the components state. The changes with the data are then notified through callbacks such as the onChange and the onClick events. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In most cases, always use controlled components.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Why do we use controlled components:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;So exactly why do we use controlled components and why are controlled components preferred over uncontrolled components?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For obvious reason, we want more control over our components. One example of when to use controlled components is when implementing forms. Using controlled components instead on uncontrolled components when implementing forms allows all the component states to be kept in the React state instead of having to solely rely on the DOM to retrieve elements value through internal states. With controlled components, when the state changes the components will programmatically re-render with the updated values. As you can imagine, this makes it undeniably easier to populate forms from the existing and available data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do we create controlled forms:&lt;/strong&gt;&lt;br&gt;
Controlled components are created by connecting the form elements to a state. We then set the attributes value to the state and to an event such as onChange or onClick.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In the screenshot below, we are initiating state for a new name and a new category. This step allows us to use states to programmatically update the current state value with the updated value.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kwyxsHPE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03xuf2tjjwhegsx36l9l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kwyxsHPE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03xuf2tjjwhegsx36l9l.png" alt="initializing state" width="800" height="135"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Here, we are making it possible for the input to change with every click of the keyboard.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xOQJmiFm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d2r2nm0689pxs71ofn9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xOQJmiFm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1d2r2nm0689pxs71ofn9.png" alt="targeting" width="800" height="206"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Then we tell the input field that when a new name or a new category is entered, a change must occur. We do this by using an onChange event and tying it to the handleNewName or handleNewCategory function.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nyXMoYiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r63h08qdy6x2x4hkayh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nyXMoYiN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r63h08qdy6x2x4hkayh5.png" alt="onChange" width="800" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Controlled components contains functions that manages the data passing into them when each onChange event occurs. The data is then saved to state and updated. That is how a controlled component works!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>components</category>
    </item>
    <item>
      <title>Introducing the Destructuring Assignment Syntax in JavaScript</title>
      <dc:creator>Tuyen Ha</dc:creator>
      <pubDate>Wed, 27 Jul 2022 00:06:53 +0000</pubDate>
      <link>https://forem.com/janestack/introducing-the-destructuring-assignment-syntax-in-javascript-52fo</link>
      <guid>https://forem.com/janestack/introducing-the-destructuring-assignment-syntax-in-javascript-52fo</guid>
      <description>&lt;p&gt;Introduced in ES6, the &lt;strong&gt;Destructuring Assignment&lt;/strong&gt; syntax is like a shortcut designed to minimize the amount of codes that are written. In short, it extracts data from both arrays and objects, making them easier to manage.&lt;/p&gt;

&lt;h4&gt;
  
  
  In this tutorial, I will go over the idea of destructuring objects and arrays.
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Destructuring Arrays:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One way of getting an element out from an array is to make that element into a variable and providing it an appropriate index.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For the array below, we want to get the first element&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6iiw6c9z09t0rg8xie8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6iiw6c9z09t0rg8xie8n.png" alt="Array ex image"&gt;&lt;/a&gt;&lt;em&gt;We made john into a variable then gave index[0] to the name variable. We are asking for the very first element to get pulled out.&lt;/em&gt;&lt;br&gt;
&lt;code&gt;const john = name[0]; &lt;br&gt;
console.log(john); // LOG: john&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So that is the way it's usually done but there is a much easier way of doing all that thanks to the destructuring syntax!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's say we wanted to grab the first three elements in the array&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Instead of doing this,&lt;br&gt;
&lt;code&gt;const john = name[0];&lt;br&gt;
const stephanie = name[1];&lt;br&gt;
const amy = name[2];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We could simply do this,&lt;br&gt;
&lt;code&gt;const [john, stephanie, amy] = name;&lt;/code&gt;&lt;br&gt;
You simply take the element that you want to destructure and place it on the right side of the equal sign then place the elements you want to grab on the inside of the array brackets.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using an array literal, each variable gets mapped to the same element at the same index of the array.&lt;br&gt;
&lt;code&gt;console.log(john); // LOG: john&lt;br&gt;
console.log(stephanie); // LOG: stephanie&lt;br&gt;
console.log(amy); // LOG: amy&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35j86vj1081zqjaaovgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35j86vj1081zqjaaovgh.png" alt="Array destructuring"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's say you wanted to skip an element and only grab index[0] and index[3]&lt;/em&gt;&lt;br&gt;
&lt;code&gt;const [john, , , amy] = name;&lt;/code&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This basically say "skip element two and three!"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Destructuring Objects:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here we have two objects, contactOne and contactTwo with the properties of name, age, and address. The address property has properties of it's own, city and state.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwanrzhqbrwwiwmgh6oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjwanrzhqbrwwiwmgh6oj.png" alt="Obj Destructuring examples"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You place the property you are trying to grab inside the curly braces and where you are grabbing it from on the right side of the equal sign.&lt;br&gt;
&lt;code&gt;const { name, age } = contactOne;&lt;br&gt;
console.log(name); // LOG: Jimmy&lt;br&gt;
console.log(age); // LOG: 41&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Destructuring objects uses {curly braces} instead of [brackets].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With Objects you could also use a default value,&lt;br&gt;
&lt;em&gt;for example:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4qnzru3gu1flti7xd9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc4qnzru3gu1flti7xd9u.png" alt="default value ex"&gt;&lt;/a&gt;&lt;code&gt;console.log(occupation); // LOG: unknown&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;All you have to do is create a new property of occupation then set the default value to "unknown." As you can see, by default, the property of occupation is set to "unknown."&lt;/p&gt;

&lt;p&gt;On the contrary, you could also add a property of occupation directly inside contactTwo and have the output of each individual contact's personal occupation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv4nc27w5alg85gmdb7x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv4nc27w5alg85gmdb7x.png" alt="more obj destructuring"&gt;&lt;/a&gt;&lt;code&gt;console.log(occupation); // LOG: Software Engineer I&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Destructuring may seem like a small deal when handling just a few parameters, but in situations where you may have a large number of parameters to work with, this method has been proven to be quite beneficial. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
